在查看“单个帖子"时,用wp_nav_menu类突出显示正确的菜单父类时出现问题 [英] Trouble highlighting correct menu parent with wp_nav_menu classes while viewing “single posts”

查看:126
本文介绍了在查看“单个帖子"时,用wp_nav_menu类突出显示正确的菜单父类时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新了我的站点上的菜单,以利用wp_nav_menu.使用WP进行设置非常简单,但是我遇到了一个小麻烦,即wordpress输出其父/祖先类以突出显示内容所属的当前页面的方式,特别是在单个帖子中.页面 ...

I just updated a menu on a site of mine to utilize wp_nav_menu. Setting this up with WP was fairly straight forward however I've run into one small snag with the the way wordpress is outputting its parent/ancestor classes for use in highlighting the current page that the content belongs to, particularly with single post pages...

只要.current_page_item a.current_page_parent a突出显示当前页面,只要它与带有孩子的普通页面一样完美,但是只要您访问 events 中的帖子, 媒体,菜单中的博客链接将突出显示,这显然是不正确的.

Highlighting the current page with .current_page_item a and .current_page_parent a works perfect as long as its just on a normal page with children, however as soon as you visit a post from events or media, the blog link in the menu is highlighted instead which is incorrect obviously.

*当查看Wordpress的输出时,有一个明显的错误是,当前页面类甚至都没有在帖子所属的正确 li标记上生成,而该标记似乎是帖子的根源.问题.

*One thing noticeably wrong when looking at Wordpress' output is that the current page classes are not even being generated on the correct li tag that the post belongs to which seems to be the root of the problem.

事件,媒体和技术,以供将来参考.博客页面均使用我编写的特殊查询,仅获取该页面的相应类别,即.

For future reference, the Events, Media, & Blog pages all use a special query I've written to only grab the respective category for that page, ie.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("category_name=media&paged=$paged");

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
</div>
<?php
endwhile;
else:
endif;

希望那是足够的信息,如果没有让我知道的话. 最好, SB

Hope thats enough info, if not let me know. Best, SB

编辑-2011年8月3日

EDIT - August 3, 2011

下面是当我说wp_nav_menu在错误的li标记上生成当前类时Im所指的屏幕快照.该帖子实际所属的菜单项以蓝色突出显示.以灰色突出显示的是wordpress决定将当前类添加到的不正确的li标签.

Below is a screen shot of what Im referring to when I say that wp_nav_menu is generating the current classes on the wrong li tag. Highlighted in Blue is the menu item that the post actually belongs to. Hightlighted in Grey is the incorrect li tag that wordpress has decided to add the current classes to instead.

http://img688.imageshack.us/img688/4180/picture2zo.png

编辑-2011年8月4日

EDIT - August 4, 2011

也许这将有助于证明到目前为止,在Hadvig的协助下,我的菜单设置如何更好?

Maybe this will help demonstrate how I have the menu setup thus far a little better w/ Hadvig's assistance?

在我的 functions.php 模板中-

<?php
// Add Custom Menu Support
if ( function_exists( 'register_nav_menu' ) ) {
    register_nav_menu( 'epr_menu', 'EPR Main Menu' );
}

function my_menu_items_hook($items, $menu, $args) {

  if ( 'epr_menu' == $menu->slug ) { // check if it is process your top menu
    if ( is_single() ) { // check if single post loaded

      if ( in_category('events') || in_category('media') ) {
        foreach ( $items as $key => $value ) {
          if ( 'blog' == $value->ID ) {
            $items[$key]->classes[] = array(); //unset classes for blog item
          }

          // add class if post from event category
          if ( in_category('events') && 'events' == $value->ID ) {
            $items[$key]->classes[] = 'current-menu-item';
          }

          // add class if post from media category
          if ( in_category('media') && 'media' == $value->ID ) {
            $items[$key]->classes[] = 'current-menu-item';
          }
        }
      }
    }
  }

  return $items;
}

add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);
?>

在我的 header.php 模板中,我这样调用菜单-

In my header.php template I'm calling the menu like so -

<div id="nav_wrapper">
    <ul id="nav">
        <?php wp_nav_menu( array( 'container' => '', 'items_wrap' => '%3$s' ) ); ?>
    </ul>
</div>


推荐答案

我想知道问题是因为您将所有帖子的帖子页面设置为Blog,而wordpress将其设为父页面(在菜单中).您可以尝试使用wp_get_nav_menu_items挂钩更改此行为.示例:

I suppose problem because you set post page as Blog and wordpress set it as parent(in your menu) for all post. You can try to change this behaviour with wp_get_nav_menu_items hook. Example:

function my_menu_items_hook($items, $menu, $args) {

  if ( 'my-menu-slug' == $menu->slug ) { // check if it is process your top menu
    if ( is_single() ) { // check if single post loaded

      if ( in_category(EVENT_CATEGORY_ID) || in_category(MEDIA_CATEGORY_ID) ) {
        foreach ( $items as $key => $value ) {
          if ( BLOG_PAGE_ID == $value->object_id ) {
            $items[$key]->classes[] = array(); //unset classes for blog item
          }

          // add class if post from event category
          if ( in_category(EVENT_CATEGORY_ID) && EVENT_PAGE_ID == $value->object_id ) {
            $items[$key]->classes[] = 'current-menu-item';
          }

          // add class if post from media category
          if ( in_category(MEDIA_CATEGORY_ID) && MEDIA_PAGE_ID == $value->object_id ) {
            $items[$key]->classes[] = 'current-menu-item';
          }
        }
      }
    }
  }

  return $items;
}

add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);

您应将EVENT_CATEGORY_ID和MEDIA_CATEGORY_ID替换为您的类别ID(或名称).另外,将EVENT_PAGE_ID和MEDIA_PAGE_ID替换为您的页面ID.用菜单栏替换"my-menu-slug".

You should replace EVENT_CATEGORY_ID and MEDIA_CATEGORY_ID with your category ids(or names). Also replace EVENT_PAGE_ID and MEDIA_PAGE_ID with your page ids. Replace 'my-menu-slug' with your menu slug.

当然,仅当您将帖子仅附加到事件",媒体"或博客"中的一个类别时,这才有效.

Of course this would work only if you attach post to only one category from Events, Media or Blog.

已更新.

这篇关于在查看“单个帖子"时,用wp_nav_menu类突出显示正确的菜单父类时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆