Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. //custom function for selecting posts based on a page parent (ne' term_id)
  2. function twk_get_children_by_id($parent_id, $post_type=MENU_CPT) {
  3. $args = array(
  4. 'posts_per_page' => -1,
  5. 'post_type' => $post_type,
  6. 'post_parent' => $parent_id,
  7. 'orderby' => 'menu_order',
  8. 'order' => 'ASC'
  9. );
  10. return get_posts( $args );
  11. }
  12.  
  13. //custom nav menu walker class for Take Action Dropdown
  14. class twk_Walker_Nav_Menu extends Walker_Nav_Menu {
  15.  
  16. function walk( $elements, $max_depth) {
  17.  
  18. $args = array_slice(func_get_args(), 2);
  19. $output = '';
  20.  
  21. if ($max_depth < -1) //invalid parameter
  22. return $output;
  23.  
  24. if (empty($elements)) //nothing to walk
  25. return $output;
  26.  
  27. $id_field = $this->db_fields['id'];
  28. $parent_field = $this->db_fields['parent'];
  29.  
  30. // flat display
  31. if ( -1 == $max_depth ) {
  32. $empty_array = array();
  33. foreach ( $elements as $e )
  34. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  35. return $output;
  36. }
  37.  
  38. $top_level_elements = array();
  39. $children_elements = array();
  40. $grandchildren_elements = array();
  41.  
  42. foreach ( $elements as $e) {
  43. if ( 0 == $e->$parent_field ) {
  44. $top_level_elements[] = $e;
  45. if ( $e->type=='post_type' && $e->object == MENU_CPT ) {
  46.  
  47. $child_posts = twk_get_children_by_id($e->object_id);
  48.  
  49. foreach ( $child_posts as $child ) {
  50.  
  51. $child = wp_setup_nav_menu_item($child);
  52. $child->post_type = 'nav_menu_item';
  53. $child->menu_item_parent = $e->$id_field;
  54. $child->object = 'custom';
  55. $child->type = 'custom';
  56. $child->ID = $e->$id_field.$child->ID;
  57. $children_elements[ $e->$id_field ][] = $child;
  58. $children_elements_classes[] = $child;
  59. }
  60. }
  61. }
  62. else {
  63. $children_elements[ $e->$parent_field ][] = $e;
  64. }
  65. }
  66.  
  67. if ( empty($top_level_elements) ) {
  68.  
  69. $first = array_slice( $elements, 0, 1 );
  70. $root = $first[0];
  71.  
  72. $top_level_elements = array();
  73. $children_elements = array();
  74. foreach ( $elements as $e) {
  75. if ( $root->$parent_field == $e->$parent_field ) {
  76. $top_level_elements[] = $e;
  77. if ( $e->type=='post_type' && $e->object == MENU_CPT ) {
  78.  
  79. $child_posts = twk_get_children_by_id($e->object_id);
  80.  
  81. foreach ( $child_posts as $child ) {
  82. $child = wp_setup_nav_menu_item($child);
  83. $child->post_type = 'nav_menu_item';
  84. $child->menu_item_parent = $e->$id_field;
  85. $child->object = 'custom';
  86. $child->type = 'custom';
  87. $child->ID = $e->$id_field.$child->ID;
  88. $children_elements[ $e->$id_field ][] = $child;
  89. $children_elements_classes[] = $child;
  90. }
  91. }
  92. } else {
  93. $children_elements[ $e->$parent_field ][] = $e;
  94. }
  95. }
  96. }
  97.  
  98. //assigning the classes to our dynamically populated posts
  99. if ( $children_elements_classes ) {
  100. _wp_menu_item_classes_by_context($children_elements_classes);
  101. }
  102.  
  103. foreach ( $top_level_elements as $e ) {
  104. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  105. }
  106.  
  107. if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
  108. $empty_array = array();
  109. foreach ( $children_elements as $orphans )
  110. foreach( $orphans as $op )
  111. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  112. }
  113.  
  114. return $output;
  115. }
  116.  
  117. /*
  118. * START ADD WRAPPER AROUND SUB MENU
  119. */
  120.  
  121. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  122. global $titleParent;
  123.  
  124. // GET VARIOUS OTHER THINGS
  125. // $pageid = get_post_meta( $item->ID, '_menu_item_object_id', true );
  126. // $background = get_field('background_color', $pageid);
  127. $indent = str_repeat( 't', $depth );
  128.  
  129. $output .= "n";
  130. $output .= $indent;
  131. $output .= "<div class='sub-menu sub-menu--level-" . $depth . "'>";
  132. $output .= "<a class='sub-menu__link' href='";
  133. $output .= get_permalink( get_page_by_title( $titleParent ) );
  134. $output .= "'>";
  135. $output .= "<h3 class='title'>";
  136. $output .= $titleParent;
  137. $output .= "</h3>";
  138. $output .= "</a>";
  139. $output .= "<ul class='sub-menu__item-container has-columns'>";
  140. }
  141.  
  142. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  143. $indent = str_repeat("t", $depth);
  144. $output .= "$indent";
  145. $output .= "</ul>";
  146. $output .= "</div>";
  147. $output .= "n";
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  155. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  156. $t = '';
  157. $n = '';
  158. } else {
  159. $t = "t";
  160. $n = "n";
  161. }
  162. $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
  163.  
  164. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  165. $classes[] = 'menu-item-' . $item->ID;
  166.  
  167. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
  168.  
  169.  
  170.  
  171. // JJ - adding 'menu-item-has-children' class if applicable
  172. $itemChildren = get_pages( array( 'child_of' => $item->object_id ) );
  173. $itemHasChildren = (count( $itemChildren ) > 0) ? true : false;
  174. $itemHasChildrenClass = ($itemHasChildren === true) ? ' menu-item-has-children' : '';
  175.  
  176.  
  177.  
  178. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  179. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . $itemHasChildrenClass . '"' : '';
  180.  
  181. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  182. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  183.  
  184. $output .= $indent . '<li' . $id . $class_names . '>';
  185.  
  186. $atts = array();
  187. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  188. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  189. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  190. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  191.  
  192. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  193.  
  194. $attributes = '';
  195. foreach ( $atts as $attr => $value ) {
  196. if ( ! empty( $value ) ) {
  197. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  198. $attributes .= ' ' . $attr . '="' . $value . '"';
  199. }
  200. }
  201.  
  202. /** This filter is documented in wp-includes/post-template.php */
  203. $title = apply_filters( 'the_title', $item->title, $item->ID );
  204.  
  205. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  206.  
  207.  
  208. // JJ - check if page redirects to an external link
  209. $openInNewTab = '';
  210. $pagePostMeta = get_post_meta( $item->object_id );
  211.  
  212. if (array_key_exists('_redirector', $pagePostMeta)) {
  213. if (strpos($pagePostMeta['_redirector'][0], 'url') !== false) {
  214. $openInNewTab = 'target="_blank"';
  215. } else {
  216. $openInNewTab = '';
  217. }
  218.  
  219. }
  220.  
  221.  
  222.  
  223. $item_output = $args->before;
  224. $item_output .= '<a'. $attributes . ' ' . $openInNewTab . '>';
  225. $item_output .= $args->link_before . $title . $args->link_after;
  226. $item_output .= '</a>';
  227. $item_output .= $args->after;
  228.  
  229. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  230. }
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement