0
  1. I have setup a custom post type called 'expertizeom'
    1. I have setup a page called "expertize" and added to my custom menu (site main nav).
    2. The page is rendered with an archive called archive-expertizeom.php

The problem is that when i browse to that page everything is working fine but wordpress isn't adding the current-menu-item class.

can you help? Thanks

Francesco

1
  • I ran into this problem a while back and found this tutorial useful - wp.tutsplus.com/tutorials/theme-development/… It walks through creating a plugin to add a meta box for adding custom post type archives to the menu and solves the current-menu-item issue. (the link to the complete plugin is down the bottom if you're after a quick fix)
    – user31525
    Apr 15, 2013 at 10:35

4 Answers 4

2

If you using wp nav menu you can use this to add the current-menu-item class put this in your theme functions.php file and remember to change "mypageslug" to your wanted current page:

function additional_active_item_classes($classes = array(), $menu_item = false){
    global $wp_query;

    if ( $menu_item->post_name == 'mypageslug' && is_page_template('archive-expertizeom.php') ) {
        $classes[] = 'current-menu-item';
    }

    return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );

This Code adds the class "current-page-item" if you are on a page with the template archive-expertizeom.php and the menu have a post_name that matches the post_name "mypageslug ie the slug of the wanted page.

1
0

In my case, i'm using magic-fields-2 plugin to create custom post types - i had just to activate the option "Hierarchical - Whether the post type is hierarchical. Allows Parent to be specified"

1
  • How does this answers the Question?
    – brasofilo
    Nov 6, 2012 at 16:27
0

I've had the same problem and this worked for me:

query_posts('cat=3');
while (have_posts()) : the_post();
    get_template_part( 'content', get_post_format() );
endwhile;
wp_reset_query();

wp_reset_query(); does all the magic :)

1
0

@pontus-abrahamsson's answer didn't quite work for me as my pages were using page.php and so I needed something else to determine whether it was the same page I was viewing.

I don't know how robust this is but it worked for me and it works with all templates (that I tried):

function additional_active_item_classes($classes = array(), $menu_item = false){
    global $wp;
    global $wp_query;

    if ( home_url( $wp->request ) == rtrim($menu_item->url,"/") ) {
        $classes[] = 'current-menu-item';
    }

    return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.