0

I am showing a wp menu with the following function:

<?php
                    wp_nav_menu(
                        array(
                            'theme_location'    => 'primary', 
                            'container'         => false,
                            'depth'             => 2,
                        ) 
                    );
                    ?>

My menu has only pages and no posts or categories. One of my page is showing posts. If i click one of the posts, I go to that post page. But problem is that, then I cant get the active menu. It make sense because it is in post page now. but how can i achieve that.

1

The below filter adds your class based on title, you could also do this by post ID if you'd like: $item->object_id == 123

function wp_nav_parent_class( $classes, $item ) {

    if( is_singular('post') && $item->title == "Your Menu Item Title Here" )
        array_push($classes, 'current_page_parent');

    return $classes;
}
add_filter('nav_menu_css_class', 'wp_nav_parent_class', 10, 2);

If is_singular('post') does not work try is_single(), but it should work. Just replace the conditional string with the menu item title of your choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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