• Hi all,

    I’m having trouble trying to highlight one menu item when it’s selected. As you can see in this page:

    https://finotratoeventos2.hospedagemdesites.ws/?cat=3

    All sub item under the portfolio menu are correctly highlighted when selected, except for the sub item “relacionamentos”.

    The problem is that the code that highlights all sub items is the same!

    <ul>
                                <li
    								<?php if ( is_page('portfolio') ) {
                                        echo 'class="atual"';
                                    } ?>
                                ><a href="index.php?page_id=18">todos</a></li>
    							<?php
    								$catsy = get_the_category();
    								$myCat = $catsy[0]->cat_ID;
                                    $args = array(
                                    'orderby'            => 'ID',
                                    'order'              => 'ASC',
                                    'style'              => 'list',
                                    'child_of'           => 9,
                                    'hierarchical'       => 1,
                                    'title_li'           => '',
    								'current_category'   => $myCat,
                                    );
                                    wp_list_categories( $args );
                                ?>
                            </ul>

    I have no idea why it doesn’t work for one particular sub item. Can you please help me?

Viewing 7 replies - 1 through 7 (of 7 total)
  • What is the name of Category 9?
    Is “relacionamentos” a child of Category 9?
    Not in this menu, but in the Post Categories, because this line in the code…
    'child_of' => 9,
    …is only listing children of Category 9.

    Thread Starter rodrego

    (@rodrego)

    Category 9 is portfolio, and relacionamento is a a child of it, I just double-checked. Otherwise, I guess it wouldn’t even be listed… and the only problem is the highlight, the category is shown and the link works correctly.

    The entire code seems fine except for this…
    'current_category' => $myCat,
    …remove the comma from behind $myCat because it is the last item of the array. Though it is syntactically correct, it may fail the list in Internet Explorer and in certain SQL instances.

    Thread Starter rodrego

    (@rodrego)

    Just did it, nothing changed… Is there anything else you’d like me to show you to help understanding this issue?

    Not that I can think of. I just inspected the entire nav with Firefox dev tools and there is nothing wrong, no errors. Honestly, I’m a bit baffled.

    have you tried to ouptut $myCat and $catsy to see what category is actually found in that moment?

    $catsy = get_the_category();
    $myCat = $catsy[0]->cat_ID;
    echo 'myCat ' . $myCat;
    var_dump( $catsy );

    unless in a single post within the loop, using the first returned element of get_the_category() can be a bit random;
    https://developer.www.ads-software.com/reference/functions/get_the_category/

    as you are in a category archive page, try

    $myCat = get_query_var( 'cat' );

    Thread Starter rodrego

    (@rodrego)

    Michael, that did it for me. I did as you said, and the returned category ID = 10: “Home”.

    And “Relacionamento” was ID = 3.

    Weird, specially since all other posts worked fine.

    I went back to the admin panel, and found out that all posts filed under category 3 were also under category 10. But that was also true to categories 2, 4, 5 and 6, which had no problem.

    But… “Relacionamento” was the only category alphabetically behind “Home. All others started with letters A, C or E.

    I changed “Home” to “Z-home”. It worked.

    I guess WordPress just picks whatever category name appears first if all posts in a category also belong to another category. You can probably call this a bug, can’t you?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trouble with current category highlight in a menu’ is closed to new replies.