[Resolved] How to set menu choice as current in certain pages

Home Forums Support [Resolved] How to set menu choice as current in certain pages

Home Forums Support How to set menu choice as current in certain pages

Viewing 15 posts - 1 through 15 (of 33 total)
  • Author
    Posts
  • #396076
    Harris

    Hello,

    I have made a custom blog page and hanged in the menu (News). When I am in that page the “News” menu choice shows as active (current).

    When I click on one of the blog posts in this page, I go to the single blog posts page and the “News” menu is not active any more (obviously).

    Is there some way I can set the “News” menu choice as current (active) both when I am in my custom blog page (list) and also when I am in any of my single posts pages?

    Thanks!

    #396114
    Leo
    Staff
    Customer Support

    Hi there,

    I believe it’s do-able but would require some pretty complicated coding.

    There is a similar request here: https://generatepress.com/forums/topic/background-colours-of-menus/ and she ended up finding someone to code it for her.

    #396170
    Harris

    Thanks!

    Isn’t that supposed to do something similar?

    <?php
    /* 
     * Customize Menu Item Classes
     * @author Bill Erickson
     * @link http://www.billerickson.net/customize-which-menu-item-is-marked-active/
     *
     * @param array $classes, current menu classes
     * @param object $item, current menu item
     * @param object $args, menu arguments
     * @return array $classes
     */
    function be_menu_item_classes( $classes, $item, $args ) {
    	if( 'header' !== $args->theme_location )
    		return $classes;
    	if( ( is_singular( 'post' ) || is_category() || is_tag() ) && 'Blog' == $item->title )
    		$classes[] = 'current-menu-item';
    		
    	if( ( is_singular( 'code' ) || is_tax( 'code-tag' ) ) && 'Code' == $item->title )
    		$classes[] = 'current-menu-item';
    		
    	if( is_singular( 'projects' ) && 'Case Studies' == $item->title )
    		$classes[] = 'current-menu-item';
    		
    	return array_unique( $classes );
    }
    add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
    #396256
    Tom
    Lead Developer
    Lead Developer

    That code is perfect ๐Ÿ™‚

    #396352
    Harris

    Any chance ot get some help to adopt it in GP?

    #396660
    Leo
    Staff
    Customer Support

    Did you try adding it or not yet?

    If not try one of the methods here: https://docs.generatepress.com/article/adding-php/

    #396685
    Harris

    Hello,
    I am trying to find what the needed parameters are in order to make it work.

    #396719
    Tom
    Lead Developer
    Lead Developer

    Is “News” a category name, or just all of your blog posts?

    #396723
    Harris

    The category name is “news” and I want to “asign” it in the menu item named “NEA”

    #396963
    Tom
    Lead Developer
    Lead Developer

    In that case, you could do this:

    function be_menu_item_classes( $classes, $item, $args ) {
        if ( 'primary' !== $args->theme_location ) {
            return $classes;
        }
    
        if ( ( is_singular( 'post' ) && in_category( 'news' ) ) && 'ฮฮ•ฮ‘' == $item->title ) {
            $classes[] = 'current-menu-item';
        }
    		
        return array_unique( $classes );
    }
    add_filter( 'nav_menu_css_class', 'be_menu_item_classes', 10, 3 );
    #397105
    Harris

    Thanks a lot Tom!
    For some reason I thought I had to change more parameters than the posts category and the menu item name.

    #397378
    Tom
    Lead Developer
    Lead Developer

    No problem! ๐Ÿ™‚

    #1105442
    Harris

    Hello!

    Can this be modified to cover single product pages (woocommerce) instead of single posts pages?

    Thanks

    #1106070
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Being on a single product should highlight the Shop menu item by default as long as the Shop menu item is pointing to the standard product archives.

    #1106109
    Harris

    It is not pointing to the standard product archives thats why I asked.
    For some reason can’t find the place to post you a link in the initial post.

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