最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

archives - Menu Disappears on Category Pages

programmeradmin1浏览0评论

I have a site whose navigation disappears on category pages for posts in the ‘post’ post type, but not for posts in any of my custom post types. The ‘post’ post type and post of the custom post types use the same template file for theirs archives and headers – no differences. The menu is using the wp_nav_menu function. Example links below (again, same template files being used):

Menu: /

No Menu:

I have a site whose navigation disappears on category pages for posts in the ‘post’ post type, but not for posts in any of my custom post types. The ‘post’ post type and post of the custom post types use the same template file for theirs archives and headers – no differences. The menu is using the wp_nav_menu function. Example links below (again, same template files being used):

Menu: http://politichicks.tv/column/

No Menu: http://politichicks.tv/category/videos

Share Improve this question asked Nov 26, 2013 at 18:10 Jacob LamontJacob Lamont 411 silver badge3 bronze badges 3
  • Theme Folder: github/AlanBreck/Vimes-Child-Theme Files of interest: github/AlanBreck/Vimes-Child-Theme/blob/master/header.php github/AlanBreck/Vimes-Child-Theme/blob/master/archive.php – Jacob Lamont Commented Nov 26, 2013 at 18:10
  • You must have figured this out. What worked? I have the same issue, only on the category page do my menus not show. – mikato Commented Jun 18, 2015 at 21:31
  • 1 this often happens when a theme or plugin has an incorrectly targeted pre_get_posts action. the query modifications intended for the main query are also applied to the query for menu items, causing no items to be returned. – Milo Commented Feb 28, 2016 at 5:36
Add a comment  | 

2 Answers 2

Reset to default 2

I know this is an old question but seems that is still unresolved. As @Milo said you may have an incorrect pre_get_posts implementation.

Most people do this like this (example):

add_filter( 'pre_get_posts', 'query_post_type' );

function query_post_type( $query ) {
    if ( is_category() ) {
        $post_type = get_query_var( 'post_type' );
        if ( $post_type ) {
            $post_type = $post_type;
        } else {
            $post_type = array( 'nav_menu_item', 'post', 'departments' );
        }
        $query->set( 'post_type', $post_type );

        return $query;
    }
}

But we must change the line: $post_type = $post_type; and pass 'nav_menu_item' to the $post_type in order to menu to display, as follow:

add_filter( 'pre_get_posts', 'query_post_type' );
function query_post_type( $query ) {
    if ( is_category() ) {
        $post_type = get_query_var( 'post_type' );
        if ( $post_type ) {
            $post_type = array( 'nav_menu_item', $post_type );
        } else {
            $post_type = array( 'nav_menu_item', 'post', 'departments' );
        }
        $query->set( 'post_type', $post_type );

        return $query;
    }
}

I hope this help people who do this wrong and sorry for my bad English.

Have you tried using the wp_nav_menu() function? here is the wp_nav_menu reference.

Instead of this:

<?php if ( has_nav_menu( 'primary' )) {
                                if(!getMainMenu('primary')){
                    /*$backup = $wp_query;
                    $wp_query = NULL;
                    $wp_query = new WP_Query(array('post_type' => 'post'));*/
                    getMainMenu('primary');
                    /*$wp_query = $backup;*/
                }
                            }

I wold try this:

<?php if ( has_nav_menu( 'primary' )) {     
    wp_nav_menu( array('menu' => 'primary' ))
}
发布评论

评论列表(0)

  1. 暂无评论