简体   繁体   English

在分类模板文件中打印自定义字段(Drupal 7)

[英]Print custom field in taxonomy template file (Drupal 7)

I'm using Drupal 7 and am trying to change the taxonomy-term.tpl.php file to display a custom field.我正在使用 Drupal 7 并尝试更改taxonomy-term.tpl.php文件以显示自定义字段。 The vocabulary has a Link field.词汇表有一个链接字段。

词汇领域

I want to print the link from field_url wrapped around the taxonomy term's name.我想打印来自围绕分类术语名称的field_url的链接。 I've tried adding a taxonomy-term.tpl.php file to my theme with the following code but I'm not having any luck:我尝试使用以下代码将taxonomy-term.tpl.php文件添加到我的主题中,但我没有任何运气:

<div id="taxonomy-term-<?php print $term->tid;?>" class="<?php print $classes;?>">
    <a href="<?php print render($content['field_url']);?>">
        <?php print render($term_name);?>
    </a>
</div>

I think I must be targeting the Link field incorrectly — any ideas on how to fix this?我想我一定是错误地定位了链接字段——关于如何解决这个问题的任何想法?

Firstly, render($content['field_url']) will render field and return html with wrapper div etc. But we need just url.首先, render($content['field_url'])将渲染字段并返回 html 和包装器 div 等。但我们只需要 url。 In template file we can obtain it from $field_url[0]['url']在模板文件中,我们可以从 $field_url[0]['url']

Secondly, $term_name contains string, there is no need to render() it.其次, $term_name包含字符串,不需要render()它。

Thus your code should turn into:因此你的代码应该变成:

<div id="taxonomy-term-<?php print $term->tid; ?>" class="<?php print $classes; ?>">
    <a href="<?php print $field_url[0]['url']; ?>">
        <?php print $term_name; ?>
    </a>
</div>

And do not forgive to clear theme cache after creating template files in your theme directory.并且不要原谅在您的主题目录中创建模板文件后清除主题缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM