如何打印块中的节点分类?

发布于 2024-10-11 05:09:42 字数 1415 浏览 2 评论 0原文

我想在节点视图页面(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹿! 2024-10-18 05:09:42

实际上,在预处理中使用以下代码可能更容易:

if ($node = menu_get_object()) {
  $vars['node_field_tags'] = field_view_field('node', $node, 'field_tags', 'full');
}

然后在模板中使用以下代码:

print render($node_field_tags);

Actually what may be easier is the following code in your preprocess:

if ($node = menu_get_object()) {
  $vars['node_field_tags'] = field_view_field('node', $node, 'field_tags', 'full');
}

And then use the following in your template:

print render($node_field_tags);
意犹 2024-10-18 05:09:42

首先,您应该检查预处理函数是否正在运行并且缓存没有与您一起使用。然后你可以尝试检查变量。我认为您不能在 $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.

红焚 2024-10-18 05:09:42

您还可以查看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.

荒路情人 2024-10-18 05:09:42

我遇到了我正在寻找的解决方案:

mytheme_preprocess_block() in template.php*

$node_content = node_view(node_load(arg(1)));
$vars['node_content'] = $node_content;

block.tpl.php

print render($node_content['field_tags']);

I've come across the solution I was looking for:

mytheme_preprocess_block() in template.php*

$node_content = node_view(node_load(arg(1)));
$vars['node_content'] = $node_content;

.

block.tpl.php

print render($node_content['field_tags']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文