国外设计欣赏网站 - DOOOOR.com

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,微信登陆

搜索

[Drupal教程] drupal覆写node中的submitted方法

[复制链接]
发表于 10-13-2011 02:43 | 显示全部楼层 |阅读模式
弄过drupal的,对blog中的submitted都不会太陌生吧,Submitted by admin on Tue, 04/12/2011 – 11:54这是其默认的一种风格,今天我们只要来看如何在drupal7中改写这种风格。4 y. e; K6 t5 V8 e
        首先我们来看看在drupal6中的实现方法:
( x: e( G, X, S5 q2 `% i$ b9 m& @5 r* [' c& q8 A. S( g' ?
1、在相应的主题下,加上下面的代码
( ]1 |8 d# H# [4 F8 J9 E& lfunction yourthemename_node_submitted($node) {return t('Posted by !username on @datetime', array('!username' => theme('username', $node),'@datetime' => format_date($node->created, 'custom', 'd M Y')));}
$ [  g" ^5 F1 G& H' J
& a. E* H: G' _  X/ w; s4 W5 t1 e5 z" ?2、然后在node.tpl.php文件中加入
5 n  r8 F5 f6 P) q  r$ L& @' l$ c<?php if ($display_submitted): ?>   <?php print $submitted ?><?php endif; ?>
: X0 `3 U( T/ K; `7 u* ~        清除缓存就能看到改过后的效果了。
% c: O: b. V' E: Y6 t& G        上面是在drupal6下的修改方法,而我们今天的主题是如何在drupal7下改写。有人会说,就按上面那种方法不行吧,在我试过之后,是没有任何效果的,后来经查询才得知,在drupal7下好像是不存在这个么下theme_node_submitted()函数。如此下来我们为了要得到效果就需要通过其他的方法。现在我总罗列几种修改的方法
& g8 R, }, F9 P
) O! D% q5 D) o8 w1 b第一种方法:* O2 ?7 L- B% @
       我们在相应主题下的template.php下加入下面的代码  [& j$ V% y" Z$ |" T# w% ]2 x
function html5_preprocess_node(&$variables) {   $variables['submitted'] = t('By !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));}% Q2 n, T4 @! K+ m7 j
      加完之后同样需要在node.tpl.php下加入# p/ K: U; a8 M- f
<?php if ($display_submitted): ?>   <?php print $submitted ?><?php endif; ?>
, x, O' Z$ y4 F9 ?  n8 f      这样才能显示出来的。
0 H  r  G4 I9 i$ E9 D, H% X
; l7 K& t9 o# j4 k第二种方法:- x- |) c. T9 F& [
       直接在node.tpl.php下修改。也就是在node.tpl.php文件中加入  b* A% P6 @' S) D
<?php if ($display_submitted): ?>    <footer class="author">       <?php            print t('By !username on !datetime',              array('!username' => $name, '!datetime' => format_date($node->created)));        ?>   </footer><?php endif; ?>% J7 ^4 k, e: U# \) |/ h
        如果还想修改日期格式,我们可以把代码换成
$ a" z; g2 P1 X; t<?php  print t('By !username on !datetime',   array('!username' => $name, '!datetime' => format_date($node->created, 'custom', 'd M Y')));?>
; b+ E6 U" x* n% ~! r
% w% V/ p, X, T" G% }+ X& \' t, n第三种方法:
) {8 t* D% x0 W' E# [        这种方法和第二种是一样的,只是我们把上面的分成了两部分,有时为了更好的布局,所以我现在拆开来放$ x+ \4 _' E. V
<?php if ($display_submitted): ?>  <footer class="author">    <div class="username">      <?php  print t('By !username',array('!username'=> $name)); ?>    </div>    <div class="date">                 <?php print t('on !datetime',array('!datetime'=>format_date($node->created, 'custom', 'Md, Y')));  ?>    </div>  </footer><?php endif; ?>" [* Y. R, r; o' _$ T4 T6 c

. X9 k  ?; B- U4 J+ U9 Y/ D9 ?
$ ~2 _8 U/ c' |       如需转载请注明出处:W3CPLUS: p6 ^6 G2 X9 G- c" X

# x% Y! T! t# i# O

|2011-2026-版权声明|平台(网站)公约|手机版|手机版|DOOOOR 设计网 ( 吉ICP备2022003869号 )

GMT+8, 12-3-2025 08:24 , Processed in 0.067321 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表