Support » Fixing WordPress » is_single OR is_category trouble

  • carlsen.rasmus

    (@carlsenrasmus)


    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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    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 ) )

    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

    carlsen.rasmus

    (@carlsenrasmus)

    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

    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

    carlsen.rasmus

    (@carlsenrasmus)

    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

    carlsen.rasmus

    (@carlsenrasmus)

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

    ras

    carlsen.rasmus

    (@carlsenrasmus)

    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

    carlsen.rasmus

    (@carlsenrasmus)

    ps: blog-excerpts.php is a template …

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘is_single OR is_category trouble’ is closed to new replies.