WordPress自定义Walker类获取子菜单的姊妹页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordPress自定义Walker类获取子菜单的姊妹页相关的知识,希望对你有一定的参考价值。

No modifications or testing carried out yet.
  1. /**
  2.  * Custom Walker to extract current sub-menu
  3.  */
  4.  
  5. class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {
  6.  
  7. var $found_parents = array();
  8.  
  9. function start_el(&$output, $item, $depth, $args) {
  10.  
  11. global $wp_query;
  12.  
  13. //this only works for second level sub navigations
  14. $parent_item_id = 0;
  15.  
  16. $indent = ( $depth ) ? str_repeat( " ", $depth ) : '';
  17.  
  18. $class_names = $value = '';
  19.  
  20. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  21.  
  22. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  23. $class_names = ' class="' . esc_attr( $class_names ) . '"';
  24.  
  25.  
  26. if ( ($item->menu_item_parent==0) && (strpos($class_names, 'current-menu-parent')) ) {
  27. $output.= '
  28. <li>';
  29. }
  30.  
  31.  
  32. // Checks if the current element is in the current selection
  33. if (strpos($class_names, 'current-menu-item')
  34. || strpos($class_names, 'current-menu-parent')
  35. || strpos($class_names, 'current-menu-ancestor')
  36. || (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) {
  37.  
  38. // Keep track of all selected parents
  39. $this->found_parents[] = $item->ID;
  40.  
  41. //check if the item_parent matches the current item_parent
  42. if($item->menu_item_parent!=$parent_item_id){
  43.  
  44. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  45.  
  46. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  47. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  48. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  49. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  50.  
  51. $item_output = $args->before;
  52. $item_output .= '<a'. $attributes .'>';
  53. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  54. $item_output .= '</a>';
  55. $item_output .= $args->after;
  56.  
  57. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  58. }
  59.  
  60.  
  61. }
  62. }
  63.  
  64. function end_el(&$output, $item, $depth) {
  65. // Closes only the opened li
  66. if ( is_array($this->found_parents) && in_array( $item->ID, $this->found_parents ) ) {
  67. $output .= "</li> ";
  68. }
  69. }
  70.  
  71. function end_lvl(&$output, $depth) {
  72. $indent = str_repeat(" ", $depth);
  73. // If the sub-menu is empty, strip the opening tag, else closes it
  74. if (substr($output, -22)=="<ul class="sub-menu"> ") {
  75. $output = substr($output, 0, strlen($output)-23);
  76. } else {
  77. $output .= "$indent</ul> ";
  78. }
  79. }
  80.  
  81. }

以上是关于WordPress自定义Walker类获取子菜单的姊妹页的主要内容,如果未能解决你的问题,请参考以下文章

wp_nav_menu walker 类子菜单仅显示在父页面上

添加一个 div 并使用 wordpress walker 更改类或导航菜单?

php Wordpress菜单/导航----将.active类添加到活动菜单项---- Bootstrap Nav Walker

Wordpress - 从自定义帖子类型中删除子菜单

如何在 WordPress 中使用 wp_get_nav_menu_items 生成自定义菜单/子菜单系统?

WordPress自定义分类父子导航菜单