[英]Move "Submitted By" from node.tpl to page.tpl
I'm looking to move "Submitted By" info from node.tpl to page.tpl however when I add the following from node.tpl I get errors.我希望将“提交者”信息从 node.tpl 移动到 page.tpl 但是当我从 node.tpl 添加以下内容时出现错误。 Im assuming I dont have access to those variables, but would like to know how I can set up a pre-proccess to get it to display as it does in the node.tpl
我假设我无权访问这些变量,但想知道如何设置预处理以使其像在 node.tpl 中一样显示
<?php if ($display_submitted): ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php endif; ?>
You can either use a preprocess function in the template.php of your theme, as explained here:您可以在主题的 template.php 中使用预处理函数,如下所述:
https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php
In your case it would look like this (tested on Drupal 7):在您的情况下,它看起来像这样(在 Drupal 7 上测试):
function yourtheme_preprocess_page(&$variables) {
$variables['author'] = "";
if (isset($variables['node']) && ($account = user_load($variables['node']->uid))) {
$variables['author'] = $account->name;
}
}
And then in your page.tpl.php use this:然后在你的 page.tpl.php 中使用这个:
Submitted by: <?php print $author; ?>
If you don't want to touch any of your theme's files, but you need to output the author's name in another region as the node content, you could create a view (block display) containing the node author, and assign it to the region.如果不想动你的主题的任何文件,但需要输出另一个区域的作者姓名作为节点内容,你可以创建一个包含节点作者的视图(块显示),并将其分配给区域.
While normally done in node.tpl.php
, if the page is a node view page, the $node variable is also available in page.tpl.php
虽然通常在
node.tpl.php
完成, node.tpl.php
如果页面是节点视图页面,则 $node 变量也在page.tpl.php
可用
You can then use something like:然后你可以使用类似的东西:
if (isset($node)) {
// Check if display submitted variable is set for this node type
if (variable_get('node_submitted_'. $node->type, 0)) {
// Do stuff
}
}
An alternative approach would be adding the required logic instead to an implementation of另一种方法是将所需的逻辑添加到
hook_preprocess_page
Bonus update: You can see the $node variable added to page.tpl.php in core template_preprocess_page
奖励更新:您可以在核心
template_preprocess_page
看到添加到 page.tpl.php 的 $node 变量
if ($node = menu_get_object()) {
$variables['node'] = $node;
}
In page.tpl.php
:在
page.tpl.php
:
<div class="submitted">
<?php echo format_date($node->created, 'custom','d.m.Y'); ?><br />
<?php echo 'by ' . $node->name; ?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.