自定义发布类型.Current-菜单项不适用于自定义发布类型存档页面

时间:2013-05-21 作者:GabLeRoux

我正在使用自定义贴子类型,但导航菜单无法获取.current-menu-item 在我的custom post archive page 菜单项。事实上,当我在那一页或任何custom posts, 不是cpt archive菜单项获取当前类,而是指向文章的菜单项获取.current-menu-item (我的博客页面)。

经过几个小时的google测试和黑客攻击,我发现了这个thread on wordpress 下面是经过编辑的解决方案修复版本的副本:

// As of WP 3.1.1 addition of classes for css styling to parents of custom post types doesn\'t exist.
// We want the correct classes added to the correct custom post type parent in the wp-nav-menu for css styling and highlighting, so we\'re modifying each individually...
// The id of each link is required for each one you want to modify
// Place this in your WordPress functions.php file

function remove_parent_classes($class)
{
  // check for current page classes, return false if they exist.
    return ($class == \'current_page_item\' || $class == \'current_page_parent\' || $class == \'current_page_ancestor\'  || $class == \'current-menu-item\') ? FALSE : TRUE;
}

function add_class_to_wp_nav_menu($classes)
{
     switch (get_post_type())
     {
        case \'artist\':
            // we\'re viewing a custom post type, so remove the \'current_page_xxx and current-menu-item\' from all menu items.
            $classes = array_filter($classes, "remove_parent_classes");

            // add the current page class to a specific menu item (replace ###).
            if (in_array(\'menu-item-171\', $classes))
            {
               $classes[] = \'current_page_parent\';
         }
            break;
     }
    return $classes;
}
add_filter(\'nav_menu_css_class\', \'add_class_to_wp_nav_menu\');
当我们使用菜单项id时,这个解决方案是有效的,但我发现它非常难看,不可能将其用于插件开发。。。

当我们在存档页面或以更干净的方式在其中一个自定义帖子上时,有没有其他想法来选择与自定义帖子存档页面相对应的菜单项?

换句话说,归档模板不应该从框中选择自己的菜单项

我从来没有想过在wordpress导航菜单中突出某个元素可能是自定义帖子类型的问题,我对此有点失望。无论如何,下面是我用来管理自定义帖子(艺术家)的代码的n表:

artist.php:

/****************************************************************
* Custom Post Type
****************************************************************/

add_action( \'init\', \'custom_post_artist\' );
function custom_post_artist()
{
  $labels = array(
    [...]
    );

  register_post_type( \'artist\',
    array(
      \'labels\' => $labels,
      \'public\' => true,
      \'menu_position\' => 15,
      \'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\', \'revisions\', \'excerpt\'),
      \'show_in_nav_menus\' => true,
      \'show_in_menu\' => true,
      \'taxonomies\' => array( \'artist_genre\', \'artist_music_type\' ),
      \'has_archive\' => \'artistes\',
      \'rewrite\' => array( \'slug\' => __(\'artistes\', \'ppdev\'), \'with_front\' => False )
      )
    );
}

/****************************************************************
* Templates
****************************************************************/

add_filter( \'template_include\', \'include_tpl_function\', 1 );
function include_tpl_function( $template_path )
{
  if ( get_post_type() == \'artist\' )
  {
    if ( is_single() )
    {
      // checks if the file exists in the theme first,
      // otherwise serve the file from the plugin
      if ( $theme_file = locate_template( array(\'single-artist.php\') ) )
      {
        $template_path = $theme_file;
      }
      else
      {
        $template_path = ARTIST_PATH . \'templates/single-artist.php\';
      }
    } 
    else if( is_archive() )
    {
      if ( $theme_file = locate_template( array(\'archive-artist.php\') ) )
      {
        $template_path = $theme_file;
      }
      else
      {
        $template_path = ARTIST_PATH . \'templates/archive-artist.php\';
      }
    }
  }
  return $template_path;
}
非常基本的存档模板页面:

archive-artist.php:

<section id="primary">
  <div id="content" role="main">
    <?php if ( have_posts() ) : ?>
    <header class="page-header">
      <h1 class="page-title">Latest artists</h1>
    </header>

      <!-- Start the Loop -->         
      <?php while ( have_posts() ) : the_post();

      $thumbnail_attr = array(
        \'class\' => "aligncenter",
        \'alt\' => get_the_title()
      );

      <h2>
        <a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
        </a>
      </h2>

      if ( has_post_thumbnail() ) : ?>
          <a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail( \'full\', $thumbnail_attr ); ?>
          </a>            
      <?php endif; ?>
      <?php endwhile; ?>

     <!-- Display page navigation -->
   <?php global $wp_query;
   if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages > 1 ) { ?>
   <nav id="<?php echo $nav_id; ?>">
     <div class="nav-previous"><?php next_posts_link( \'<span class="meta-nav">&larr;</span> Previous artists\' ) ); ?></div>
     <div class="nav-next"><?php previous_posts_link( \'Next artists <span class= "meta-nav">&rarr;</span>\' ); ?></div>
   </nav>
   <?php };
  endif; ?>
 </div>
</section>
注:也测试了十二个主题和同一期

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

我通过搜索类似的帖子和链接找到了答案。我添加了一行内容来满足我的需要(我想防止我的博客页面在发表自定义帖子时变得更高)。请参见此行:unset($classes[array_search(\'current_page_parent\',$classes)]);

Solution

function add_parent_url_menu_class( $classes = array(), $item = false ) {
  // Get current URL
  $current_url = current_url();

  // Get homepage URL
  $homepage_url = trailingslashit( get_bloginfo( \'url\' ) );

  // Exclude 404 and homepage
  if( is_404() or $item->url == $homepage_url )
    return $classes;

  if ( get_post_type() == "artist" )
  {
    unset($classes[array_search(\'current_page_parent\',$classes)]);
    if ( isset($item->url) )
      if ( strstr( $current_url, $item->url) )
        $classes[] = \'current-menu-item\';
  }

  return $classes;
}

function current_url() {
  // Protocol
  $url = ( \'on\' == $_SERVER[\'HTTPS\'] ) ? \'https://\' : \'http://\';
  $url .= $_SERVER[\'SERVER_NAME\'];

  // Port
  $url .= ( \'80\' == $_SERVER[\'SERVER_PORT\'] ) ? \'\' : \':\' . $_SERVER[\'SERVER_PORT\'];
  $url .= $_SERVER[\'REQUEST_URI\'];

  return trailingslashit( $url );
}
add_filter( \'nav_menu_css_class\', \'add_parent_url_menu_class\', 10, 3 );

Source

我不喜欢这个答案,因为它使用url来比较菜单项中的元素,但这个可以很好地用于不同的菜单ID,可以在插件中使用。希望它能帮助一些人,因为一些我认为是wordpress bug的东西损失了几个小时。

SO网友:user1575949

我在扩展Walker_Nav_Menu 是否跳过检查current_page_parent 当C.P.T.I确定C.P.T之后stackexchange 邮递

这并不能完全解决我使用的问题current_page_item 对于C.P.T父菜单项,但它确实实现了预期的结果。这是我的class.

class My_Menu extends Walker_Nav_Menu {  

  public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

    $post_type = get_post_type();
    $class_names = \'\';

    // Check if post_type - $item->attr_title returns the WP Menu custom link Title Attribute
    $class_names .= ($item->attr_title != \'\' && $item->attr_title == $post_type || in_array("current_page_item", $item->classes)) ? \'current_page_item\' : \'\';

    // Prevent Blog menu item from getting the current_page_parent class when Custom Post Type
    if(!is_post_type_archive() && !is_singular(array( \'custom-one\', \'custom-two\'))){
    $class_names .= in_array("current_page_parent", $item->classes) ? \' current_page_parent\' : \'\';
    }

    $id = apply_filters( \'nav_menu_item_id\', \'menu-item-\'. $item->ID, $item, $args );
    $id = $id ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
    $class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';

    $output .= \'<li\' . $id . $class_names .\'>\';

    $atts = array();
    $atts[\'title\']  = ! empty( $item->attr_title ) ? $item->attr_title : \'\';
    $atts[\'target\'] = ! empty( $item->target )     ? $item->target     : \'\';
    $atts[\'rel\']    = ! empty( $item->xfn )        ? $item->xfn        : \'\';
    $atts[\'href\']   = ! empty( $item->url )        ? $item->url        : \'\';

    $atts = apply_filters( \'nav_menu_link_attributes\', $atts, $item, $args );

    $attributes = \'\';
    foreach ( $atts as $attr => $value ) {
        if ( ! empty( $value ) ) {
            $value = ( \'href\' === $attr ) ? esc_url( $value ) : esc_attr( $value );
            $attributes .= \' \' . $attr . \'="\' . $value . \'"\';
        }
    }
    $item_output = $args->before;
    $item_output .= \'<a\'. $attributes .\'>\';
    $item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
    $item_output .= \'</a>\';

    $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args ); 
  }

  public function end_el( &$output, $item, $depth = 0, $args = array() ) {
    $output .= "</li>\\n";
  }

} // Walker_Nav_Menu

结束

相关推荐

如果使用PHP的后果包括TEMPLATEPATH?

我已经使用设置API创建了一个主题选项页面,所以现在我需要使用CSS来设置它的样式。而是在函数内部使用类似的函数。phpfunction mytheme_add_init() { if ( is_admin() ) { $get_path=get_bloginfo(\'template_directory\'); wp_enqueue_style(\"functions\", $get_path.\"/css/admin.css\", false, \"1.0\",