Forums

Problem in dynamic menu highlighting (6 posts)

  1. julious
    Member
    Posted 4 years ago #

    It's a problem to create a dynamic menu in my blog.
    Used as a documentation: http://codex.wordpress.org/Dynamic_Menu_Highlighting

    My code:

    <?php /* Dynamic Menu Highlighting - CSS */
    if ( is_home() ) { $current = 'home'; }
    
    elseif ( in_category(10) || in_category (11) || in_category (12) ) { $current = 'new'; }
    
    elseif ( in_category(20) || in_category (21) ) { $current = 'games'; }
    
    else { $current = 'others'; }
    ?>

    The menu worked, but the problem is when I make a publication in two different categories parents, then if I published the same post in the category 'games' (20), sub-category 'online games' (21) and also in the category child 'news' (12) that is within the category default (10), and when user enters the category games (www.myblog.com/category/games), instead of highlighting the link games, highlighting the link default (as if you were on (www.myblog.com/category/default).

    I think I get it correct, is missing define any further condition in the code, someone could help me?

  2. julious
    Member
    Posted 4 years ago #

    Is another thing that I forgot to ask been in fact a mistake of the programming of the code of the theme it is or if it is a bug of the wordpress?

  3. Adam Brown
    Member
    Posted 4 years ago #

    Whichever category gets picked up first in your if...elseif...elseif chain is the one that will be highlighted. This is not a bug. That's just how PHP works.

    If you don't want to highlight category 10 unless category 20 is not also checked, then change the order of the conditions:

    <?php /* Dynamic Menu Highlighting - CSS */
    if ( is_home() ) { $current = 'home'; }
    
    elseif ( in_category(20) || in_category (21) ) { $current = 'games'; }
    
    elseif ( in_category(10) || in_category (11) || in_category (12) ) { $current = 'new'; }
    
    else { $current = 'others'; }
    ?>
  4. julious
    Member
    Posted 4 years ago #

    But the mistake inverts like this just, therefore when it is in http://www.myblog.com/category/default and the last post it is of the category default and game, then it will go highlight you game.

    Doesn't have as quite so to solve? Because me when I work with PHP using querry strings there is the definition of variables:
    In games.php: $page = "games"
    In home.php: $page = "default"

    In code:
    if ( $page == 'games' ) { echo "Games"; }
    elseif ( $page == 'default' ) { echo "Home"; }
    else { echo "Error - No category"; }

  5. Adam Brown
    Member
    Posted 4 years ago #

    Apparently I'm not understanding your question.

  6. julious
    Member
    Posted 4 years ago #

    I only wanted to know if is there a way for when it is for instance in category/games highlight the link of the category games, even if the last post to be in other categories.

    Because of the way that you suggested me, it will change the place error, when it is in the category default, but it will continue.

Topic Closed

This topic has been closed to new replies.

About this Topic