将"Submitted By"从node.tpl移动到page.tpl



我希望将"Submitted By"信息从node.tpl移动到page.tpl,但当我从node.tpl添加以下信息时,我会出现错误。我假设我无法访问这些变量,但我想知道如何设置一个预处理程序,使其像在node.tpl 中那样显示

  <?php if ($display_submitted): ?>
    <div class="submitted">
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

您可以在主题的template.php中使用预处理函数,如下所述:

https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php

在你的情况下,它看起来是这样的(在Drupal 7上测试(:

function yourtheme_preprocess_page(&$variables) {
  $variables['author'] = "";
  if (isset($variables['node']) && ($account = user_load($variables['node']->uid))) {
    $variables['author'] = $account->name;
  }
}

然后在你的页面.tpl.php中使用这个:

Submitted by: <?php print $author; ?>

如果您不想触摸主题的任何文件,但需要在另一个区域中输出作者的姓名作为节点内容,则可以创建一个包含节点作者的视图(块显示(,并将其分配给该区域。

虽然通常在node.tpl.php中完成,但如果页面是节点视图页面,$node变量也可在page.tpl.php 中使用

然后你可以使用类似的东西:

if (isset($node)) {
  // Check if display submitted variable is set for this node type
  if (variable_get('node_submitted_'. $node->type, 0)) {
    // Do stuff
  }
}

另一种方法是将所需的逻辑添加到的实现中

hook_preprocess_page

奖励更新:您可以在核心template_preprocess_page 中看到$node变量添加到page.tpl.php

if ($node = menu_get_object()) {
  $variables['node'] = $node;
}

page.tpl.php:中

<div class="submitted">
     <?php echo format_date($node->created, 'custom','d.m.Y'); ?><br />
     <?php echo 'by ' . $node->name; ?>
</div>

最新更新