[Resolved] Setting primary navigation menu item as current

Home Forums Support [Resolved] Setting primary navigation menu item as current

Home Forums Support Setting primary navigation menu item as current

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #814955
    Roger

    Hi, I have set a custom blog posts page titled ‘news’ and want the ‘news’ navigation item to remain selected as current for all individual blog posts, category pages and tag pages.

    I adapted the code from this topic and added it as an inside_navigation hook but it doesn’t appear to be working.

    <?php
    function be_menu_item_classes( $classes, $item, $args ) {
    	if( 'header' !== $args->theme_location )
    		return $classes;
    	if( ( is_singular( 'post' ) || is_category() || is_tag() ) && 'news' == $item->title )
    		$classes[] = 'current-menu-item';
    		
    	return array_unique( $classes );
    }
    add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
    ?>

    What am I doing wrong?

    #815029
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You need to add it using one of these methods: https://docs.generatepress.com/article/adding-php/

    Also, this line is assuming the menu has a header theme location, which GP doesn’t have:

    if( 'header' !== $args->theme_location )

    You’ll want to replace header with primary.

    #815107
    Roger

    Perfect, thanks Tom ๐Ÿ™‚

    #815770
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.