0

I have two custom post types, and I have trouble adding the current-menu-item to both. In my functions.php I have following code:

    function additional_active_item_classes($classes = array(), $menu_item = false){
    global $wp_query;
    if(in_array('current-menu-item', $menu_item->classes)){
        $classes[] = 'current-menu-item';
    }
    if ( $menu_item->post_name == 'portfolio' && is_post_type_archive('portfolio') ) {
        $classes[] = 'current-menu-item';
    }
    if ( $menu_item->post_name == 'portfolio' && is_singular('portfolio') ) {
        $classes[] = 'current-menu-item';
    }   
    if ( $menu_item->post_name == 'freebies' && is_post_type_archive('freebies') ) {
        $classes[] = 'current-menu-item';
    }
    if ( $menu_item->post_name == 'freebies' && is_singular('freebies') ) {
        $classes[] = 'current-menu-item';
    }
    return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 4 );

I know this is maybe not the most optimized code but most important to get it work. You can see the site here:

http://codingowl.net/

On portfolio it's working but not on the freebies. Why? Thanks so much in advance

Edit:

Here is how I create the custom post type freebies and it's taxonomy, freebie-tags:

function my_custom_post_freebies() {
  $labels = array(
    'name'               => _x( 'Freebies', 'post type general name' ),
    'singular_name'      => _x( 'freebie', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'freebie' ),
    'add_new_item'       => __( 'Add New Freebie' ),
    'edit_item'          => __( 'Edit Freebie' ),
    'new_item'           => __( 'New Freebie' ),
    'all_items'          => __( 'All Freebies' ),
    'view_item'          => __( 'View Freebie' ),
    'search_items'       => __( 'Search Freebies' ),
    'not_found'          => __( 'No Freebies found' ),
    'not_found_in_trash' => __( 'No Freebies found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Freebies'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our freebie items',
    'public'        => true,
    'menu_position' => 6,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'taxonomies'    => array('category'),      
    'has_archive'   => true,
  );
  register_post_type( 'freebies', $args ); 
}
add_action( 'init', 'my_custom_post_freebies' );    

function add_custom_freebietags() {
    register_taxonomy('freebie-tags', 'freebies', array(
        'hierarchical' => true,
        'labels' => array(
            'name' => _x( 'Freebie-tags', 'taxonomy general name' ),
            'singular_name' => _x( 'Freebie-Tag', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search tags' ),
            'all_items' => __( 'All tags' ),
            'parent_item' => __( 'Parent tag' ),
            'parent_item_colon' => __( 'Parent tag:' ),
            'edit_item' => __( 'Edit tag' ),
            'update_item' => __( 'Update tag' ),
            'add_new_item' => __( 'Add new tag' ),
            'new_item_name' => __( 'New tag name' ),
            'menu_name' => __( 'Freebie-tags' ),
        ),
        'rewrite' => array(
            'slug' => 'freebie-tags',
            'with_front' => false,
            'hierarchical' => true
        ),
    ));
}
add_action( 'init', 'add_custom_freebietags', 0 ); 
  • according to the existing classes, freebies is a taxonomy term archive, not a post type archive. – Milo Jan 1 '15 at 17:13
  • Thanks @Milo! Sorry I do not understand, I have them both created the same way register_post_type( 'portfolio', $args ); + add_action( 'init', 'my_custom_post_portfolio' ); and register_post_type( 'freebies', $args ); + add_action( 'init', 'my_custom_post_freebies' ); - they have both same arguments? – Owl Jan 1 '15 at 18:23
  • I added the code where I create the custom post type and it's taxonomy to the initial post. From my understanding, are the portfolio and freebies just custom post types. The taxonomy terms are different from them. – Owl Jan 1 '15 at 18:36
  • looking at the freebies menu item, it has the classes menu-item-type-taxonomy and menu-item-object-category. how did you add that menu item to the menu? – Milo Jan 1 '15 at 18:40
  • Ha good question, I think I had them both via "categories" menu in Wordpress, see screenshot attached: fotos-hochladen.net/uploads/screenshot8p9dyc0bvx.jpg – Owl Jan 1 '15 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.