基于wpu navu菜单而不是页面结构的Wordpress面包屑

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于wpu navu菜单而不是页面结构的Wordpress面包屑相关的知识,希望对你有一定的参考价值。

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. }

以上是关于基于wpu navu菜单而不是页面结构的Wordpress面包屑的主要内容,如果未能解决你的问题,请参考以下文章

向wpu navu menu()中的当前页面项添加类

向wpu navu menu()中的当前页面项添加类

功能para personalizer wpu navu menu()

PHP Wordpress面包屑基于wp_nav_menu而不是页面结构

WordPress自定义wpu列表u页面输出

单击管理面板侧菜单刷新页面而不是展开菜单项