2016-10-04 44 views
0

我已經創建了一個自定義的帖子類型「演示文稿」,我在主菜單中添加了存檔頁面。 我層級的菜單是:WordPress的:如何在CPT菜單項目中包含'當前菜單祖先/父'類?

會議(空鏈接:HREF = 「#」)

- 公司(空鏈接:HREF = 「#」)

- - 演示文稿(CPT檔案頁)

我想補充班「當前菜單始祖」和「當前菜單父」,具體如下:

會議(空鏈接:HREF =「#」)目前的菜單-ancestor'

- 興業(空鏈接:HREF = 「#」) '當前菜單父'

- - 演示(CPT檔案頁)當前菜單

我怎樣才能做到這一點? 謝謝

回答

0

不確定這是什麼,你正在尋找,但你可以修改此條件,使條件適合你。 嘗試print_r();$classes , $item, $args

此外,檢查Codex references

function filter_handler($classes , $item, $args, $depth){ 
    if (in_array('current-menu-item', $item->classes) OR in_array('current_page_item', $item->classes)) { 
     if ('presentations' == get_post_type()) { 
      if ($depth == 0) { 
       $classes[] = 'current-menu-ancestor'; 
      } 
      if ($depth == 1) { 
       $classes[] = 'current-menu-parent'; 
      } 
      if ($depth == 2) { 
       $classes[] = 'current-menu'; 
      } 
     } 
    } 
    return $classes; 
} 
add_filter('nav_menu_css_class', 'filter_handler', 10, 4); 
0

謝謝!

我用您的代碼如下:

function filter_handler($classes , $item, $args, $depth){ 
if ('presentations' == get_post_type()){ 
    if ($item->post_name == 'meetings' || $item->post_name == 'companies' || $item->post_name == 'presentations-arch') { 
     if ($depth == 0) { 
     $classes[] = 'current-menu-ancestor'; 
     } 
     if ($depth == 1) { 
     $classes[] = 'current-menu-parent'; 
     } 
     if ($depth == 2) { 
     $classes[] = 'current-menu-item'; 
     } 
    } 
} 
return $classes; 

} 的add_filter( 'nav_menu_css_class', 'filter_handler',10,4);

它的工作原理! 但是,還有其他解決方案更「動態」嗎?我不想使用項目菜單的名稱。

你有什麼想法嗎?

相關問題