0
votes

I am interested in how to highlight the category in which I am present - with the "current" class in the template. The problem is compounded by the fact that the list of categories is not retrieved from the database WordPress (the same place, as I understand, they are stored?), that is - directly from the HTML... How to make the following code:

code:

<ul>
    <li class="blabla">
        <a>1</a>
    </li>
    <li class="blabla">
        <a>2</a>
    </li>
    <li class="blabla">
        <a>3</a>
    </li>
</ul>

code to be dynamic, for example when I go into the category of "2" to "li" will be added to the class "current":

code:

<ul>
    <li class="blabla">
        <a>1</a>
    </li>
    <li class="blabla current">
        <a>2</a>
    </li>
    <li class="blabla">
        <a>3</a>
    </li>
</ul>

I tried to read the codex but I have two problems - I'm not good at php and most importantly hard to read in English

I sincerely apologize for the broken English with which I asked, "that" the question

2

2 Answers

2
votes

I'm not sure if I understand your question, but based on what you said, you can use css.

Example

/* This will highlight your menu item gray if it has current in its class attribute*/
.current {
    background:#555;
}

If you just want a menu of the categories, you can use the Wordpress category widget. It creates a menu with a list of all the categories. If you click on the category, it will have a class of current-cat. This only happens when you are in the category filter list view, so if you click on a post with that category, it will not say 'current-cat'.

If you want something more specialized, then it's still possible. Tell me if the category widget will not work for you.


Here is the way to do it in php if you don't want to use a widget. It does basically the same thing, but you have more freedom where you want to put it.

This is the reference page: http://codex.wordpress.org/Template_Tags/wp_list_categories

Basically all you need is that function. Just place this snippet of code where you want your categories to appear:

<?php echo wp_list_categories() ?> 

That should create a basic html list of all the categories. It will also set the current-cat class if you are in the category archive view.

If you want to override the current category behavior above, you can also force the menu to select a current category based on the id of the category you select. So in this example, it will add the current-cat class to the category with id three.

<?php echo wp_list_categories(array('current_category' => 3)) ?>
0
votes
/* this is working #747474:gray*/

.current {
    background:#747474;
}