国外设计欣赏网站 - 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中改写这种风格。
: c% F# Y( l7 @3 A3 d        首先我们来看看在drupal6中的实现方法:
; |* G* R, ?- o) p) e" D9 r
# L: P3 ~/ T' r. w/ q1、在相应的主题下,加上下面的代码
- L7 P3 p6 B2 \* k4 ^function 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')));}
, A8 m6 B; J& l& D0 R: ]9 Z) i: ^! j0 q$ |3 U6 `4 ^1 [
2、然后在node.tpl.php文件中加入; J& q0 Q4 B7 M% S' Z6 ^
<?php if ($display_submitted): ?>   <?php print $submitted ?><?php endif; ?>
; }+ C# n8 o9 r( `3 k- \  J        清除缓存就能看到改过后的效果了。- t7 {% S; M" k
        上面是在drupal6下的修改方法,而我们今天的主题是如何在drupal7下改写。有人会说,就按上面那种方法不行吧,在我试过之后,是没有任何效果的,后来经查询才得知,在drupal7下好像是不存在这个么下theme_node_submitted()函数。如此下来我们为了要得到效果就需要通过其他的方法。现在我总罗列几种修改的方法" {  c  |' @' i7 Z0 V

5 I) u, e# ]9 f$ A' f第一种方法:2 d- m% R+ y( J$ {* R
       我们在相应主题下的template.php下加入下面的代码% x* H0 w. u, c% g: z9 N3 a' a
function html5_preprocess_node(&$variables) {   $variables['submitted'] = t('By !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));}
- F4 k% [* l2 \- M8 b4 k* ^      加完之后同样需要在node.tpl.php下加入
4 n' }  ^- M" T: K$ O/ J$ [<?php if ($display_submitted): ?>   <?php print $submitted ?><?php endif; ?>
  `- ]8 ?. ^9 h( ^$ D      这样才能显示出来的。$ l, a* P, i$ z% f  Z8 X% {! I
* B1 ?3 I7 v% g  ~2 q& n; q
第二种方法:
" t3 X8 z+ h# J/ a* Y; q* h* y! B       直接在node.tpl.php下修改。也就是在node.tpl.php文件中加入
. T8 b/ ^  r( U: X2 e7 s<?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; ?>) O- w1 F/ C+ ^3 [
        如果还想修改日期格式,我们可以把代码换成& x( Q. Z! l5 _- w
<?php  print t('By !username on !datetime',   array('!username' => $name, '!datetime' => format_date($node->created, 'custom', 'd M Y')));?>
6 K" }1 r9 K0 C" e1 C: s6 J. h, [- G5 z! E
第三种方法:
; p4 W5 [# `, F3 _: c. m        这种方法和第二种是一样的,只是我们把上面的分成了两部分,有时为了更好的布局,所以我现在拆开来放2 w+ V, r) k& B& c- v; @3 ]
<?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; ?>
( a7 W: t0 _. O% j* _+ q" D6 l8 ?; U7 w
) I# Z; s- w( |
       如需转载请注明出处:W3CPLUS
1 U9 Z3 }; X4 t! x3 Q( j
6 K' X1 U. B4 B) ^  i  C

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

GMT+8, 11-12-2025 14:07 , Processed in 0.313103 second(s), 441 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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