如何覆写node中的submitted

        弄过drupal的,对blog中的submitted都不会太陌生吧,Submitted by admin on Tue, 04/12/2011 – 11:54这是其默认的一种风格,今天我们只要来看如何在drupal7中改写这种风格。

        首先我们来看看在drupal6中的实现方法:

        1、在相应的主题下,加上下面的代码

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')
  ));
}

      

       2、然后在node.tpl.php文件中加入

<?php if ($display_submitted): ?>
   <?php print $submitted ?>
<?php endif; ?>

 

        清除缓存就能看到改过后的效果了。

        上面是在drupal6下的修改方法,而我们今天的主题是如何在drupal7下改写。有人会说,就按上面那种方法不行吧,在我试过之后,是没有任何效果的,后来经查询才得知,在drupal7下好像是不存在这个么下theme_node_submitted()函数。如此下来我们为了要得到效果就需要通过其他的方法。现在我总罗列几种修改的方法

       第一种方法:

       我们在相应主题下的template.php下加入下面的代码

	
function html5_preprocess_node(&$variables) {
   $variables['submitted'] = t('By !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));
}

 

      加完之后同样需要在node.tpl.php下加入

<?php if ($display_submitted): ?>
   <?php print $submitted ?>
<?php endif; ?>

 

      这样才能显示出来的。

       第二种方法:

       直接在node.tpl.php下修改。也就是在node.tpl.php文件中加入

<?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; ?>

 

        如果还想修改日期格式,我们可以把代码换成

<?php
  print t('By !username on !datetime',
    array('!username' => $name, '!datetime' => format_date($node->created, 'custom', 'd M Y')));
?>

 

       第三种方法:

        这种方法和第二种是一样的,只是我们把上面的分成了两部分,有时为了更好的布局,所以我现在拆开来放

<?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; ?>

 

       大家可以去尝试一下,如果有更好的办法记得告诉我哟。

       如需转载请注明出处:W3CPLUS

返回顶部