简体   繁体   中英

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. The vocabulary has a Link field.

词汇领域

I want to print the link from field_url wrapped around the taxonomy term's name. I've tried adding a taxonomy-term.tpl.php file to my theme with the following code but I'm not having any luck:

<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. In template file we can obtain it from $field_url[0]['url']

Secondly, $term_name contains string, there is no need to render() it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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