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!

Answers


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.


Need Your Help

NSTreeController KVO issue

cocoa key-value-observing nstreecontroller

I have a NSTreeController which array is bound to a "items" (custom) property of an NSArrayController subclass.

Select onchange update textarea multiple

javascript jquery html select onchange

I am using the following two scripts to update a textarea in a form with the selected value from the dropdown. In isolation they both work correctly however, I am trying to combine them so that cha...