Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. <?php
  2. /*
  3. * REGISTER NAVIATION MENUS
  4. *************************************************************/
  5. add_action( 'after_setup_theme', 'register_make_nav_menus' );
  6. function register_make_nav_menus() {
  7. register_nav_menus( array(
  8. 'header-menu' => 'Main Header Menu',
  9. ) );
  10. }
  11.  
  12. class make_submenu_class extends Walker_Nav_Menu {
  13. /**
  14. * [start_lvl Starts the list before the elements are added.]
  15. * The $args parameter holds additional values that may be used with the child class methods.
  16. * This method is called at the start of the output list.
  17. * The start level refers to the start of a sub-level. Mening that the output does not effect the initial wrapping around the whole navigation, but only the list of childerns children.
  18. *
  19. * @param [string] &$output (Required) Used to append additional content (passed by reference).
  20. * @param [int] $depth (Required) Depth of the item.
  21. * @param [array] $args (Optional) An array of additional arguments. Default value: array()
  22. *
  23. */
  24. function start_lvl(&$output, $depth = 0, $args = array()) {
  25. $output .= ($depth == 0) ? "<div class=\"megamenu\">\n" : ''; // Wrap the sub-menu items with the megamenu wrapper
  26.  
  27. $indent = str_repeat("\t", $depth);
  28. $output .= "\n$indent<ul class=\"sub-menu\">\n"; // add a numeric value to determin the level of submenu
  29. }
  30. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  31. $output .= ($depth == 0) ? "</div><!-- /.megamenu -->\n" : ''; // Close the megamenu wrapper at the end of the first level
  32.  
  33. }
  34.  
  35. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  36.  
  37. $classes = empty ( $item->classes ) ? array () : (array) $item->classes;
  38. $class_names = join( ' ', apply_filters(
  39. 'nav_menu_css_class', array_filter( $classes ), $item
  40. )
  41. );
  42.  
  43. ! empty ( $class_names )
  44. and $class_names = ' class="'. esc_attr( $class_names ) . '"';
  45.  
  46. if ( in_array( 'featured', $item->classes ) ) {
  47. // Wrap the featured items to seperate them from the main sub items
  48. $output .= "</ul>\n<ul class=\"sub-menu-featured\">\n";
  49. }
  50. $output .= "<li id='menu-item-$item->ID' $class_names>";
  51.  
  52. $attributes = '';
  53.  
  54. ! empty( $item->attr_title )
  55. and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
  56. ! empty( $item->target )
  57. and $attributes .= ' target="' . esc_attr( $item->target ) .'"';
  58. ! empty( $item->xfn )
  59. and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"';
  60. ! empty( $item->url )
  61. and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
  62.  
  63. $title = apply_filters( 'the_title', $item->title, $item->ID );
  64.  
  65. $item_output = $args->before
  66. . "<a $attributes>"
  67. . $args->link_before
  68. . $title
  69. . '</a> '
  70. . $args->link_after
  71. . $args->after;
  72.  
  73. // Since $output is called by reference we don't need to return anything.
  74. $output .= apply_filters(
  75. 'walker_nav_menu_start_el'
  76. , $item_output
  77. , $item
  78. , $depth
  79. , $args
  80. );
  81. }
  82. function end_el( &$output, $item, $depth = 0, $args = array() ) {
  83. if ( in_array( 'featured', $item->classes ) ) {
  84. // Close off the .feature item
  85. $output .= "</ul>";
  86. }
  87. }
  88. }
  89.  
  90. /**
  91. * Add the images submenu items with the featured class
  92. *
  93. * @param [array] $items List of menu objects (WP_Post).
  94. * @return [array]
  95. */
  96. function make_featured_submenu_items( $items ) {
  97. $featured_item_ids = array();
  98. //echo '<pre>' . var_export($items, true) . '</pre>';
  99. foreach ( $items as $item ) {
  100. if ( $item->menu_item_parent != 0 ) { // Don't add the featured image if it's a parent item.
  101. if ( in_array( 'featured', $item->classes, true )
  102. && isset( $item->ID )
  103. && has_post_thumbnail( $item->object_id )) {
  104.  
  105. $item->title = sprintf(
  106. '%1$s %2$s %3$s %4$s',
  107. '<span class="featured-title">' . $item->title .'</span>',
  108. get_the_post_thumbnail( $item->object_id, 'thumbnail', array( 'alt' => esc_attr( $item->title ) ) ),
  109. '<span class="featured-excerpt">' . wp_trim_words(get_the_excerpt($item->object_id), 5) . '</span>', // Limit to 5 words, doesn't need to be too lengthy
  110. '<a href="' . get_post_type_archive_link( $item->object ) . '">See all ' . $item->type_label . '<span class="arrow-button"></span></a>'
  111. );
  112. }
  113. }
  114. }
  115.  
  116. return $items;
  117. }
  118. add_filter( 'wp_nav_menu_objects', 'make_featured_submenu_items' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement