Support » Themes and Templates » using is_page and is_category with wp_get_nav_menu_items

  • I made a dynamic nav bar for a clients site which uses wp_get_nav_menu_items to output the menu, then I use is_page($item->object_id) || is_category($item->object_id) and add a class of “cur” if these are true.

    The problem i’m having is when on the home page, which has an id of 4, the “cur” class is applied to that item and also a category with an id of 4. I even tried something like:

    <?php $items = wp_get_nav_menu_items(5); ?>
    <?php foreach($items as $item) { ?>
    <?php $cur = false; ?>
    <?php if( (is_page() && is_page($item->object_id)) || (is_category() && is_category($item->object_id)) ) $cur = true;
    ... irrelevant markup below ...

    Anybody have any ideas why it’s adding the class to the page with an id of 4 AND the category?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello. First I am going to suggest why this might be failing. Then I’m going to make a suggestion that you try it another way.

    First the problem you are having is that you are asking

    if ( is_page() && is_page($item->object_id) ) {

    is_page(), is_category(), and most similar template related function “is_” functions have to do with what page you are ON (i.e. what page is being displayed).

    However, the object_id of the home link in the menu appears to be the same as the menu id it is given (unlike posts/pages/categories where the object_id is the post/page id or the taxonomy_id). So in your case that home link in the menu is being assigned a value of ‘4’ (this is mostly conjecture). So is it possible that the “home page” has an id of “4” and is a page? Or more coincidentally the page you assigned to the home page (assuming you are using a static home page)?

    Though this wouldn’t explain the fact that it is saying yes to both the page and the category condition. But I would definitely look at the id’s of the pages and categories in the install for a clue.

    However, since you seem to just be attempting to mark the current menu item as being the current page you are on I am going to ask if you have tried using wp_nav_menu()?

    http://codex.wordpress.org/Function_Reference/register_nav_menu
    http://codex.wordpress.org/Function_Reference/wp_nav_menu

    It automatically outputs classes indicating the current menu item for the current page being viewed as well as other useful stuff if you are doing drop down or heirarchal menus. Personally I love it.

    Optionally you can register the menu with WordPress explicitly:

    register_nav_menu('hd_menu', 'Masthead Menu'); //primary site nav

    Then in the template where you want to output it (once you’ve made it and assigned it in the Admin menu screen).

    wp_nav_menu( array(
        'theme_location'    => 'hd_menu',
        'container'         => false,
        'container_class'   => false,
        'menu_id'           => 'hd_menu_list',
        'sort_column'       => 'menu_order',
        'depth'             => '4',
        'fallback_cb'     => 'kstWpNavMenuFallbackCb'
        ) );

    And even if you don’t want to use wp_nav_menu(), you could just make the area where the menu is a widget area, add the menu to a widget in that area, and you get the same convenient markup and css classes.

    You can see an example and check out the markup and css here on my demo site. The masthead is using wp_nav_menu() and the footer in the lower left is a widget area with a menu it.

    http://beingzoe.com/zui/wordpress/kitchen_sink/kitchen_sink_html5_base_demo/

    Hope that helps. I would love to know why that was happening with your original code though. Very strange and frustrating I am sure.

    Thread Starter AndySpartan

    (@andyspartan)

    Hi Zoe, thanks for your reply. I have used wp_nav_menu in the past but found it quite limiting when trying to do a few bespoke things my designer likes to put in

    The home page is a static page with an ID of 4, and the category also has an ID of 4. I’ve been doing some playing around and I’ve sussed it 🙂

    Each nav item object contains an ‘object’ member which is either ‘page’ ‘post’ or ‘category’ – So my logic statement is now:

    <?php if( ( ($item->object == 'page') && (is_page($item->object_id)) ) || ( ($item->object == 'category') && (is_category($item->object_id)) ) ) $cur = true; ?>

    Thanks for your help!

    Glad you worked it out.

    I would love to see some examples of what is easier not using wp_nav_menu(). Anything to up the quality of my own work 😉

    Take care.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘using is_page and is_category with wp_get_nav_menu_items’ is closed to new replies.