0

Hello I'm in a bit of a trouble. I don't know what's wrong with what I'm doing. I want to add class to current category. So my php looks like this:

    <menu id="nav">
        <ul>   
            <?php $cat_id = get_cat_ID();
            foreach( $categories as $c ):?>         
            <li class="<?php if(($c->term_id) == $cat_id){echo 'active' ;} ?>">
            <a href="<?php echo get_category_link( $c->term_id ); ?>" title="<?php echo $c->cat_name ;?>">
            <?php echo $c->cat_name ;?>
            </a>                
            </li>
        <?php endforeach;   ?>  
        </ul>
</menu>

I just want to add active class to the current category. But this is not working.

2 Answers 2

1

You can use get_queried_object(), which will return category object.

See documentation:

https://codex.wordpress.org/Function_Reference/get_queried_object

1
  • Thanks for your suggestion in the end I just had to add this:
    – Mark
    Commented Oct 25, 2016 at 8:32
0

Put your $c term (in your loop) in a print_r/var_dump to see if the property you are trying to compare the values of is of the actual value that you are looking for (See below).

I've also added a ternary operator in the place of your if statement, it is just better practice. (Read below for more on ternary operators).

<menu id="nav">
    <ul>   
    <?php $cat_id = get_cat_ID();
    foreach( $categories as $c ):?>
        <?php print_r($c); ?>         
        <li class="<?php echo $c->term_id == $cat_id ? 'active' : null ;} ?>">
        <a href="<?php echo get_category_link( $c->term_id ); ?>" title="<?php echo $c->cat_name ;?>">
        <?php echo $c->cat_name ;?>
        </a>                
        </li>
    <?php endforeach;   ?>  
    </ul>
</menu>

Ternary Operator

In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

if(1==1) echo 'true'; 
else echo 'false';

can be done in a ternary operator like so:

echo 1==1 ? 'true' : 'false';

Another example

if(1==1) $boolean = true;
else $boolean = false;

can be done in a ternary operator like so:

$boolean = 1==1 ? true : false;

Since PHP 5.3, the 'middle part' of the operator can be left out. Shorthand ternary operators can be used to match only false

if(1!=2) echo 'false';

The ternary operator:

echo 1!=2 ?: 'false';

Read more: http://php.net/manual/en/language.operators.comparison.php

2
  • Thank you for your help. I saw that $cat_id is always 0, so I wanted to know how would I get $cat_id of the active category?
    – Mark
    Commented Oct 25, 2016 at 8:20
  • Wordpress uses "the" in most functions that require little to no parameters and refer to the current data available on the page. Depending on what you have available I would suggest taking a look at get_the_category(); or if you are using pretty permalinks you may be able to make use of get_query_var(); based on whether or not the category ID is available in your query.
    – Matthew
    Commented Oct 25, 2016 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.