Support » Fixing WordPress » Custom Post Types in wp nav menu help

  • I am using the below code to add custom post types to my wp nav menu and it works fine. But I only want the custom post types to show if they have posts. Right now it will show the custom post type I’ve created regardless if it has any posts. Any ideas?
    Here is the function:

    function custom_page_menu($menu, $args) {
     // get supplied args
     $list_args = $args;
     // Overide some menu settings
     $list_args['container'] = '';
     $list_args['echo'] = false;
     $list_args['title_li'] = '';
     $list_args['show_home'] = false;
     $list_args['exclude'] = 4; // excluding the homepage as I am manually adding it to the start below
     // get the current page object as we will need to refer to it when setting current items below
     global $wp_query;
     $current_page = $wp_query->get_queried_object();  
     // Loop through the custom post types and add them to the menu
     'public'   => true,
     '_builtin' => false,
     'hierarchical' => true
     )) as $pt) {
     $obj_pt = get_post_type_object($pt);
     $list_args['post_type'] = $pt;
     $menu .= '<li>'.'<a href="  ' . $obj_pt->rewrite['slug'] . ' ">' . $obj_pt->labels->name . '</a>';
     $menu .= '</li>';
     return $menu;
    add_filter( 'wp_page_menu', 'custom_page_menu' ,10,2 );

  • The topic ‘Custom Post Types in wp nav menu help’ is closed to new replies.