如何打印块中的节点分类?
我想在节点视图页面(在 Zen 子主题中)的块中打印分类术语(来自字段 field_tags
)。
所以我所做的是。
template.php
function michal_preprocess_block(&$vars, $hook) {
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
$vars['node'] = $node;
$vars['node_field_tags'] = $node->field_tags;
$vars['node_content_field_tags'] = $node->content['field_tags'];
}
}
但是,当我尝试在 block.tpl.php
中打印它时,这两个变量都没有从该字段输出分类术语。
print render($node_content_field_tags);
print render($node_field_tags);
您知道用于呈现分类术语字段的 Drupal 函数吗?
编辑 2011 年 1 月 13 日,00:21
据我了解(来自 此,这个和那个)过程代码应该看起来或多或少像这样
$node = node_load(arg(1));
$node_view($node) // Generates an array for rendering a node, see http://api.drupal.org/api/drupal/modules--node--node.module/function/node_view/7
$vars['node'] = $node;
,然后在 block.tpl.php
中:
render($node->content['field_tags']);
但是,$node->content 为 null。
你知道我错过了什么吗?
I'd like to print taxonomy terms (from field field_tags
) in a block on a node view page (in a Zen subtheme).
So what I did was.
template.php
function michal_preprocess_block(&$vars, $hook) {
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
$vars['node'] = $node;
$vars['node_field_tags'] = $node->field_tags;
$vars['node_content_field_tags'] = $node->content['field_tags'];
}
}
However, when I try to print it in block.tpl.php
, neither of these 2 variables outputs taxonomy terms from the field.
print render($node_content_field_tags);
print render($node_field_tags);
Do You know a Drupal function to render a taxonomy terms field?
EDIT 13.01.2011, 00:21
As far as I understood (from this, this and that) the process the code should look more/less like this
$node = node_load(arg(1));
$node_view($node) // Generates an array for rendering a node, see http://api.drupal.org/api/drupal/modules--node--node.module/function/node_view/7
$vars['node'] = $node;
and then in the block.tpl.php
:
render($node->content['field_tags']);
The $node->content is null, however.
Do You know what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,在预处理中使用以下代码可能更容易:
然后在模板中使用以下代码:
Actually what may be easier is the following code in your preprocess:
And then use the following in your template:
首先,您应该检查预处理函数是否正在运行并且缓存没有与您一起使用。然后你可以尝试检查变量。我认为您不能在
$node->field_tags
上使用render()
并且我不太确定$node->content ['field_tags']
要么。检查变量将帮助您弄清楚这一点,devel 在 Drupal 7 上工作得很好并且可以帮助您。
First of all, you should check that the preprocess function is being run and that cache is not playing with you. Then you could try to inspect the variables. I don't think you can use
render()
on the$node->field_tags
and I'm not too sure about$node->content['field_tags']
either.Inspecting the variables will help you figure it out, devel works fine for Drupal 7 and can help you there.
您还可以查看CCK 块模块。它创建一个与每个节点一起显示的侧边栏块(如果它有内容),并将该块添加到每个字段的渲染目标列表中,就像“teaser”、“full”和“rss”一样。
它可能不具备您正在寻找的所有控制功能,但它可能是一个不错的起点。
You might also check out the CCK Blocks module. It creates a sidebar block that displays alongside each node (if it has content), and adds that block to the list of rendering destinations for each field, just like 'teaser' and 'full' and 'rss'.
It may not have all the control you're looking for but it could be a good place to start.
我遇到了我正在寻找的解决方案:
mytheme_preprocess_block() in template.php*
。
block.tpl.php
I've come across the solution I was looking for:
mytheme_preprocess_block() in template.php*
.
block.tpl.php