WordPress.org

Ready to get started?Download WordPress

Forums

is_single OR is_category trouble (9 posts)

  1. carlsen.rasmus
    Member
    Posted 2 years ago #

    Hi,

    To highlight a menu-item (BLOG) when a single post, a category or the archive is viewed, I have this:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(),
    $menu_item = false ) {
        if ( is_single() && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) ) {
            $classes[] = 'rasmus-test';
        }
        return $classes;

    It does add the class rasmus-test to that specifik menuitem when a single post is viewed (it has ID 44).

    If i change it to this:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(),
    $menu_item = false ) {
        if ( is_category() && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) ) {
            $classes[] = 'rasmus-test';
        }
        return $classes;

    It works just fine as well for viewing a category.

    But - if I do this:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(),
    $menu_item = false ) {
        if ( is_single() || is_category()
    && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) ) {
            $classes[] = 'rasmus-test';
        }
        return $classes;

    ... the class rasmus-test is added to every menu-item.

    What is wrong?

    A bonus-question:

    Im not happy about this:

    && 44 == $menu_item->ID

    I like it to be dynamick; Like based on the pagetemplate. How do I do that?

    thx

    Rasmus

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://php.net/manual/en/language.operators.precedence.php

    try:

    if ( (is_single() || is_category())
    && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) )
  3. Digital Raindrops
    Member
    Posted 2 years ago #

    Hmmmm!

    I like it to be dynamick; Like based on the pagetemplate. How do I do that?

    is_template_page( $filename) should do it!

    This is not tested, using a page template file and should only add the class if the template is used!

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(), $menu_item = false ) {
       if( is_page_template('filename.php') && 44 == $menu_item->ID ) {
          if ( ! in_array( 'current-menu-item', $classes ) ) {
    			$classes[] = 'rasmus-test';
          }
       }
       return $classes;
    }

    HTH

    David

  4. carlsen.rasmus
    Member
    Posted 2 years ago #

    thanks! How blind can I be!

    this did it:

    if ( (is_single() || is_category())
    && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) )

    However - I dont get how to incert the menu-item ID by the templatename ...

    this:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(), $menu_item = false ) {
        if ( (is_single() || is_category || is_archive())
    && ( is_page_template('blog-excerpt.php'))
    && ! in_array( 'current-menu-item', $classes ) ) {
            $classes[] = 'rasmus-test';
        }
        return $classes;
    }

    ... wont work. It highligt all menu-items except the one base on blog-excerpt ;-)

    ras

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    If 'blog-excerpt.php' is not a page template then that will not work, it would add it to all menu items because you have not specified which menuitem id, ie: 44

    So that brings you back to:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(), $menu_item = false ) {
        if ( (is_single() || is_category || is_archive()) && 44 == $menu_item->ID ) {
    		if( !in_array( 'current-menu-item', $classes ) ) {
    			$classes[] = 'rasmus-test';
    		}
    	}
        return $classes;
    }

    HTH

    David

  6. carlsen.rasmus
    Member
    Posted 2 years ago #

    then I can just stay with:

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 4 );
    
    function add_custom_class( $classes = array(), $menu_item = false ) {
        if ( (is_single() || is_category || is_archive())
    && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) ) {
            $classes[] = 'rasmus-test';
        }
        return $classes;
    }

    it works just fine - however, in af child-theme it would be better for later theme-use, that it was based on something else than "44".

    ras

  7. carlsen.rasmus
    Member
    Posted 2 years ago #

    sorry - it does'nt work. It highight the menu-item (with id 44) all the time ...

    ras

  8. carlsen.rasmus
    Member
    Posted 2 years ago #

    Strange: this works as it should,

    add_filter( 'nav_menu_css_class', 'add_custom_class', 10, 5 );

    function add_custom_class( $classes = array(), $menu_item = false ) {
    if ( (is_single() || is_category())
    && 44 == $menu_item->ID
    && ! in_array( 'current-menu-item', $classes ) ) {
    $classes[] = 'rasmus-test';
    }
    return $classes;
    }
    but if I ad the is_archive ... it highlights the menu-item all the time.

    ras

  9. carlsen.rasmus
    Member
    Posted 2 years ago #

    ps: blog-excerpts.php is a template ...

Topic Closed

This topic has been closed to new replies.

About this Topic