WordPress.org

Forums

Highlight current category in wordpress (3 posts)

  1. Jaroslv
    Member
    Posted 3 years ago #

    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. alchymyth
    Forum Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Conditional_Tags
    http://codex.wordpress.org/Conditional_Tags#A_Category_Page

    example:

    <li <?php if( is_category('2') ) echo 'class="blabla current"'; ?>>
            <a>2</a>

    if you want the current also when a single post with the category is shown:

    <li <?php if( is_category('2') || in_category('2') ) echo 'class="blabla current"'; ?>>
            <a>2</a>
  3. Jaroslv
    Member
    Posted 3 years ago #

    alchymyth, YOU can not even imagine how much I really appreciate you! Seriously, take a sincere thank you from me

Topic Closed

This topic has been closed to new replies.

About this Topic