Posted By

adambundy on 07/20/13


Tagged

wordpress breadcrumbs wpnavmenu


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

adambundy


Wordpress breadcrumbs based on wp_nav_menu instead of page structure


 / Published in: PHP
 

URL: http://wordpress.stackexchange.com/questions/50425/show-current-navigation-path-from-menu

Thanks to Stephen Harris for this > http://wordpress.stackexchange.com/questions/50425/show-current-navigation-path-from-menu

  1. /*
  2. --- new walker class to create breadcrumb from nav menu structure using wp_nav_menu -------
  3.  
  4. use like:
  5.  
  6. wp_nav_menu( array(
  7.   'container' => 'none',
  8.   'theme_location' => 'primary',
  9.   'walker'=> new bi_BreadCrumbWalker,
  10.   'items_wrap' => '<div id="breadcrumb-%1$s" class="%2$s">%3$s</div>'
  11. ) );
  12.  
  13.  
  14. */
  15. class bi_BreadCrumbWalker extends Walker{
  16.  
  17. var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  18.  
  19. /**
  20.   * @see Walker::$db_fields
  21.   * @var array
  22.   */
  23. var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  24.  
  25. /**
  26.   * delimiter for crumbs
  27.   * @var string
  28.   */
  29. var $delimiter = ' > ';
  30.  
  31. /**
  32.   * @see Walker::start_el()
  33.   *
  34.   * @param string $output Passed by reference. Used to append additional content.
  35.   * @param object $item Menu item data object.
  36.   * @param int $depth Depth of menu item.
  37.   * @param int $current_page Menu item ID.
  38.   * @param object $args
  39.   */
  40. function start_el(&$output, $item, $depth, $args) {
  41.  
  42. //Check if menu item is an ancestor of the current page
  43. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  44. $current_identifiers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );
  45. $ancestor_of_current = array_intersect( $current_identifiers, $classes );
  46.  
  47.  
  48. if( $ancestor_of_current ){
  49. $title = apply_filters( 'the_title', $item->title, $item->ID );
  50.  
  51. //Preceed with delimter for all but the first item.
  52. if( 0 != $depth )
  53. $output .= $this->delimiter;
  54.  
  55. //Link tag attributes
  56. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  57. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  58. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  59. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  60.  
  61. //Add to the HTML output
  62. //$output .= '<a'. $attributes .'>'.$title.'</a>';
  63.  
  64. // added by me to remove the link on the current page item
  65. if ( in_array("current-menu-item", $classes) ) {
  66. $output .= '<span class="current-page">'.$title.'</span>';
  67. } else {
  68. $output .= '<a'. $attributes .'>'.$title.'</a>';
  69. }
  70.  
  71. }
  72. }
  73. }

Report this snippet  

You need to login to post a comment.