Posted By

Huskie on 09/09/11


Tagged

menu class pages wordpress custom sub Walker sister sub-menu


Versions (?)

WordPress Custom Walker Class to get Sister Pages for Sub-Menu


 / Published in: PHP
 

URL: http://darrenhuskie.com/

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( "\t", $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>\n";
  68. }
  69. }
  70.  
  71. function end_lvl(&$output, $depth) {
  72. $indent = str_repeat("\t", $depth);
  73. // If the sub-menu is empty, strip the opening tag, else closes it
  74. if (substr($output, -22)=="<ul class=\"sub-menu\">\n") {
  75. $output = substr($output, 0, strlen($output)-23);
  76. } else {
  77. $output .= "$indent</ul>\n";
  78. }
  79. }
  80.  
  81. }

Report this snippet  

You need to login to post a comment.