简体   繁体   中英

Print a link based on the taxonomy term or id in Drupal 7

I have a list of links on page.tpl.php and I would like to have one link display on nodes that is using one taxonomy term id. The taxonomy term id is 9.

Ex.

<ul class="top-menu-new upper-menu-new">
<li> <a>NEWS</a>&nbsp;&nbsp;|&nbsp;&nbsp;  </li>
<li> <a href="">BLOG</a>&nbsp;&nbsp;|&nbsp;&nbsp;  </li>
<li> <a href="">FAQ</a>&nbsp;&nbsp; | &nbsp;&nbsp;</li>

<?php if the node is using taxonomy term id 9
<li><a href=">PRIVATE PAGE</a> </li>
<?php else: ?>
<li><a href="">PUBLIC PAGE</a> </li>
<?php endif; ?>

Can anyone help?

Thanks!

In your theme's template.php ; Using template_preprocess_page (), your code should look like that

function [YOUR_THEME]_preprocess_page(&$vars)
{
    if(isset($vars['node']))
    {
        $node = $vars['node'];
        $nodeLanguage = "und"; // or use any language
        if(isset($node->field_YOUR_FIELD_REFERENCE[$nodeLanguage]))
        {
            foreach($node->field_YOUR_FIELD[$nodeLanguage] as $key => $val)
            {
                if($val['tid'] == "9")
                    $vars['found_my_term'] = true;
            }
        }
    }
}

Then, in your page template page.tpl.php :

<?php if($found_my_term) { ?>
<li><a href=">PRIVATE PAGE</a> </li>
<?php } else { ?>
<li><a href="">PUBLIC PAGE</a> </li>
<?php } ?>

The code is about checking if the current page is a node page , then checks if the node has your taxonomy field reference (In this code sample I used field_YOUR_FIELD_REFERENCE as the field name), and at last checks if it has a value of 9 .

And if so, you are storing the result in a variable (I called found_my_term ) so you can use in your page templates.

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