WP_NAV_MENU未正确显示在类别页面上

时间:2012-07-08 作者:Zain Baloch

这是我用来调用wp\\u nav\\u菜单的代码

<?php
     wp_nav_menu(
            array(
                \'container_class\' => \'menu-header\',
                \'theme_location\' => \'\',
                \'menu_id\'         => 6,
                \'depth\' => 4
            )
        );
?>
该菜单在主页和单个帖子页面上显示得非常完美,如下所示:http://www.enkitec.com/

但当我访问分类页面时,例如。http://www.enkitec.com/education/菜单项发生变化,似乎有些菜单项没有出现。

note: 我在仪表板中只有1个菜单,它被设置为默认菜单

strange thing is: 上面的代码包含在标题中。php,所以它在所有页面上都是相同的,但在类别页面上的结果仍然不同。

任何帮助都将不胜感激

此外,我还包括我的主题页代码:

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="<?php bloginfo( \'charset\' ); ?>" />
    <title><?php wp_title(\'-\', true, \'right\'); ?> <?php bloginfo(\'name\'); ?></title>
    <meta name="description" content="<?php echo is_single() ? single_post_title(\'\', true):bloginfo(\'name\')." - ".bloginfo(\'description\'); ?>" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo(\'stylesheet_url\'); ?>" />
    <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/font_kit/stylesheet.css" type="text/css" charset="utf-8" />
    <link rel="shortcut icon" href="<?php bloginfo(\'template_url\'); ?>/favicon.ico" />
    <link rel="pingback" href="<?php bloginfo(\'pingback_url\'); ?>" />
    <script type="text/javascript" src="<?php bloginfo(\'template_url\'); ?>/js/jquery.min.js"></script>
    <script type="text/javascript" src="<?php bloginfo(\'template_url\'); ?>/js/jquery.cycle.all.js"></script>
    <script type="text/javascript" src="<?php bloginfo(\'template_url\'); ?>/js/jquery-textFill-0.1.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $(\'#slider\')
        .after(\'<div id="controls">\')
        .cycle({
            fx: \'fade\',
            speed:  2500,
            timeout: 5000,
            pager: \'#controls\',
            slideExpr: \'a\'
        });
    });
    </script>
    <?php if (is_singular() && get_option(\'thread_comments\')) wp_enqueue_script(\'comment-reply\'); ?>
    <?php wp_head(); ?>
</head>
<body>
    <div id="custom_wrapper">
        <div class="header">
            <div class="left">
                <div class="logo"></div>
            </div><!--left-->
            <div class="right">
                <div class="image"></div>
            </div><!--right-->
        </div><!--header-->

        <div class="menu">
            <div class="left"></div>
            <div class="inner" id="menu">
                <?php
                    wp_nav_menu(
                        array(
                            \'container_class\' => \'menu-header\',
                            \'theme_location\' => \'\',
                            \'menu_id\'         => 6,
                            \'depth\' => 4
                        )
                    );
                ?>
            </div><!--inner-->
            <div class="right"></div>
        </div><!--menu-->
        <div class="clear_10px"></div>

        <?php $single_cat_title = single_cat_title(); ?>
        <?php $p_title = trim(wp_title(NULL, FALSE)); ?>
        <?php $cat = get_the_category(); $c_title = trim($cat[0]->name); ?>
category.php

        <div id="content" class="categories">
            <div class="left border_right">
                <?php if (have_posts()) : ?>
                <?php while(have_posts()) : the_post(); ?>

                    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
                    <?php // the_content(); ?>
                    <?php the_excerpt(); ?> 

                    <div class="clear_10px border_bottom"></div>

                    <div class="meta">
                        <?php the_time(\'F jS, Y\') ?>,
                        By <?php the_author_posts_link(); ?>.
                        Filed Under: <?php the_category(\', \'); ?>
                    </div><!--meta-->

                    <div class="clear">&nbsp;</div>

                <?php endwhile; ?>

                <?php if (  $wp_query->max_num_pages > 1 ) : ?>
                    <div id="nav-below" class="navigation">
                        <div class="nav-previous"><?php next_posts_link(__(\'&laquo; Older posts\', \'\')); ?></div>
                        <div class="nav-next"><?php previous_posts_link(__(\'Newer posts &raquo;\', \'\')); ?></div>
                    </div><!-- #nav-below -->
                    <div class="clear">&nbsp;</div>
                <?php endif; ?>

                <?php else : ?>

                    <h2>Nothing Found</h2>
                    <p>Sorry, but you are looking for something that isn\'t here.</p>
                    <p><a href="<?php echo get_option(\'home\'); ?>">Return to home page</a></p>
                    <div class="clear_10px border_bottom"></div>

                <?php endif; ?>

                <?php if (is_home()) : ?>
                    <div class="check_us_out"></div>
                <?php endif; ?>

                <div class="clear"></div>
            </div><!--left-->

            <div class="right">
                <?php get_sidebar(); ?>
            </div><!--right-->
        </div><!--content-->

<?php get_footer(); ?>
        <div id="content" class="categories">
            <div class="left border_right">
                <?php if (have_posts()) : ?>
                <?php while(have_posts()) : the_post(); ?>

                    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
                    <?php // the_content(); ?>
                    <?php the_excerpt(); ?> 

                    <div class="clear_10px border_bottom"></div>

                    <div class="meta">
                        <?php the_time(\'F jS, Y\') ?>,
                        By <?php the_author_posts_link(); ?>.
                        Filed Under: <?php the_category(\', \'); ?>
                    </div><!--meta-->

                    <div class="clear">&nbsp;</div>

                <?php endwhile; ?>

                <?php if (  $wp_query->max_num_pages > 1 ) : ?>
                    <div id="nav-below" class="navigation">
                        <div class="nav-previous"><?php next_posts_link(__(\'&laquo; Older posts\', \'\')); ?></div>
                        <div class="nav-next"><?php previous_posts_link(__(\'Newer posts &raquo;\', \'\')); ?></div>
                    </div><!-- #nav-below -->
                    <div class="clear">&nbsp;</div>
                <?php endif; ?>

                <?php else : ?>

                    <h2>Nothing Found</h2>
                    <p>Sorry, but you are looking for something that isn\'t here.</p>
                    <p><a href="<?php echo get_option(\'home\'); ?>">Return to home page</a></p>
                    <div class="clear_10px border_bottom"></div>

                <?php endif; ?>

                <?php if (is_home()) : ?>
                    <div class="check_us_out"></div>
                <?php endif; ?>

                <div class="clear"></div>
            </div><!--left-->

            <div class="right">
                <?php get_sidebar(); ?>
            </div><!--right-->
        </div><!--content-->

<?php get_footer(); ?>

functions.php

<?php
    /* Register a Custom Post Type (Slide) */
    add_action(\'init\', \'slide_init\');
    function slide_init() {
        $labels = array(
            \'name\'                  => _x(\'Slides\', \'post type general name\'),
            \'singular_name\'         => _x(\'Slide\', \'post type singular name\'),
            \'add_new\'               => _x(\'Add New\', \'slide\'),
            \'add_new_item\'          => __(\'Add New Slide\'),
            \'edit_item\'             => __(\'Edit Slide\'),
            \'new_item\'              => __(\'New Slide\'),
            \'view_item\'             => __(\'View Slide\'),
            \'search_items\'          => __(\'Search Slides\'),
            \'not_found\'             => __(\'No slides found\'),
            \'not_found_in_trash\'    => __(\'No slides found in Trash\'), 
            \'parent_item_colon\'     => \'\',
            \'menu_name\'             => \'Slides\'
        );
        $args = array(
            \'labels\'                => $labels,
            \'public\'                => true,
            \'publicly_queryable\'    => true,
            \'show_ui\'               => true, 
            \'show_in_menu\'          => true, 
            \'query_var\'             => true,
            \'rewrite\'               => true,
            \'capability_type\'       => \'post\',
            \'has_archive\'           => true, 
            \'hierarchical\'          => false,
            \'menu_position\'         => null,
            \'supports\'              => array(\'title\', \'excerpt\', \'thumbnail\')
        ); 
        register_post_type(\'slide\', $args);
    }

    /* Update Slide Messages */
    add_filter(\'post_updated_messages\', \'slide_updated_messages\');
    function slide_updated_messages($messages) {
        global $post, $post_ID;
        $messages[\'slide\'] = array(
            0 => \'\',
            1 => sprintf(__(\'Slide updated.\'), esc_url(get_permalink($post_ID))),
            2 => __(\'Custom field updated.\'),
            3 => __(\'Custom field deleted.\'),
            4 => __(\'Slide updated.\'),
            5 => isset($_GET[\'revision\']) ? sprintf(__(\'Slide restored to revision from %s\'), wp_post_revision_title((int) $_GET[\'revision\'], false)) : false,
            6 => sprintf(__(\'Slide published.\'), esc_url(get_permalink($post_ID))),
            7 => __(\'Slide saved.\'),
            8 => sprintf(__(\'Slide submitted.\'), esc_url(add_query_arg(\'preview\', \'true\', get_permalink($post_ID)))),
            9 => sprintf(__(\'Slide scheduled for: <strong>%1$s</strong>. \'), date_i18n(__(\'M j, Y @ G:i\'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
            10 => sprintf(__(\'Slide draft updated.\'), esc_url(add_query_arg(\'preview\', \'true\', get_permalink($post_ID)))),
        );
        return $messages;
    }

/* Update Slide Help */
add_action(\'contextual_help\', \'slide_help_text\', 10, 3);
function slide_help_text($contextual_help, $screen_id, $screen) {
    if (\'slide\' == $screen->id) {
        $contextual_help =
        \'<p>\' . __(\'Things to remember when adding a slide:\') . \'</p>\' .
        \'<ul>\' .
        \'<li>\' . __(\'Give the slide a title. The title will be used as the slide\\\'s headline.\') . \'</li>\' .
        \'<li>\' . __(\'Attach a Featured Image to give the slide its background.\') . \'</li>\' .
        \'<li>\' . __(\'Enter text into the Visual or HTML area. The text will appear within each slide during transitions.\') . \'</li>\' .
        \'</ul>\';
    }
    elseif (\'edit-slide\' == $screen->id) {
        $contextual_help = \'<p>\' . __(\'A list of all slides appears below. To edit a slide, click on the slide\\\'s title.\') . \'</p>\';
    }
    return $contextual_help;
}
if (function_exists(\'add_theme_support\')) {
    add_theme_support(\'post-thumbnails\');
}


function checkFeild($a) {
    if (trim($a)==""||$a==NULL||empty($a)) {
        return false;
    } else {
        return true;
    }
}

if (function_exists(\'register_sidebar\')) {
    register_sidebar(array(
        \'before_widget\' => \'<div class="block">\',
        \'after_widget\' => \'</div><!--block-->\',
        \'before_title\' => \'<h1>\',
        \'after_title\' => \'</h1>\',
    ));
}

function register_my_menus(){
    register_nav_menus(
        array(
            \'header-menu\'=>__(\'Header Menu\')
        )
    );
}
add_action(\'init\', \'register_my_menus\');

function fix_missing_menu($query) {
    $query->set(\'post_type\', array(\'post\', \'nav_menu_item\'));
}
add_filter(\'pre_get_posts\', \'fix_missing_menu\');
?>

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

不调用导航菜单menu_id. 相反,定义一个显式theme_location 对于主题模板中要包含导航菜单的每个位置,然后使用该菜单theme_location 作为传递给的参数wp_nav_menu().

在本例中,您定义了header-menu 主题位置,因此更改此选项:

<?php
 wp_nav_menu(
        array(
            \'container_class\' => \'menu-header\',
            \'theme_location\' => \'\',
            \'menu_id\'         => 6,
            \'depth\' => 4
        )
    );
?>
。。。对此:

<?php
 wp_nav_menu(
        array(
            \'container_class\' => \'menu-header\',
            \'theme_location\' => \'header-menu\',
            \'depth\' => 4
        )
    );
?>
另外:您的粘贴category.php 代码省略get_header(). 请确认您正在呼叫get_header() 在里面category.php.

SO网友:amit

看看这两个页面的源代码,在主页上我可以看到分配给“LI元素”的“Menu Item”类,而在分类页面上,是“page Item”,这意味着你的菜单在主mage上运行正常,但在分类页面上出现了问题,默认fall back menu - \'“wp\\u page\\u菜单”出现。

尝试通过向数组中添加另一个参数来禁用回退-\'fallback_cb\' => false, 看看是否有效。

结束

相关推荐

Custom menus not showing

作为我上一次关于菜单的未解决查询的后续,这个问题已经进一步扩展。我的菜单没有打印代码中的任何地方。我正在注册菜单功能。php:add_action( \'after_setup_theme\', \'your_newtheme_setup\' ); if ( ! function_exists( \'your_newtheme_setup\' ) ) : function your_newtheme_setup() { if (