1414 */
1515class User_Locations_Frontend {
1616
17- /**
17+ /**
1818 * @var Parisi_Functions The one true Parisi_Functions
1919 * @since 1.0.0
2020 */
@@ -31,13 +31,12 @@ public static function instance() {
3131 }
3232
3333 function init () {
34- // Hook in the location menu ( not OOP so it can easily be removed/moved )
35- add_action ( 'genesis_after_header ' , 'ul_do_location_menu ' , 20 );
3634
3735 // Hook in the location posts ( not OOP so it can easily be removed/moved )
3836 add_action ( 'genesis_after_loop ' , 'ul_do_location_posts ' );
3937
4038 add_filter ( 'body_class ' , array ( $ this , 'location_content_body_class ' ) );
39+ add_filter ( 'wp_get_nav_menu_items ' , array ( $ this , 'replace_primary_navigation ' ), 10 , 2 );
4140 add_filter ( 'genesis_post_info ' , array ( $ this , 'maybe_remove_post_info ' ), 99 );
4241 add_filter ( 'genesis_post_meta ' , array ( $ this , 'maybe_remove_post_meta ' ), 99 );
4342
@@ -60,6 +59,101 @@ public function location_content_body_class( $classes ) {
6059 return $ classes ;
6160 }
6261
62+ /**
63+ * Replace the primary navigation with the location menu items.
64+ *
65+ * @since 1.3.1
66+ *
67+ * @param array $items The existing menu items.
68+ * @param array $menu The menu.
69+ *
70+ * @return array The modified menu items.
71+ */
72+ public function replace_primary_navigation ( $ items , $ menu ) {
73+
74+ // Bail if not location page.
75+ if ( ! is_singular ( 'location_page ' ) ) {
76+ return $ items ;
77+ }
78+
79+ // Get all displayed locations.
80+ $ locations = get_nav_menu_locations ();
81+
82+ // Bail if no primary nav displaying.
83+ if ( ! isset ( $ locations ['primary ' ] ) ) {
84+ return $ items ;
85+ }
86+
87+ // Bail if not filtering the primary nav.
88+ if ( $ locations ['primary ' ] != $ menu ->term_id ) {
89+ return $ items ;
90+ }
91+
92+ // Get the top level parent.
93+ $ ancestors = array_reverse ( get_ancestors ( get_the_ID (), get_post_type () ) );
94+ $ ancestors = empty ( $ ancestors ) ? array ( get_the_ID () ) : $ ancestors ;
95+
96+ return $ this ->get_menu_tree ( $ ancestors [0 ], 'location_page ' );
97+ }
98+
99+ /**
100+ * Get the menu tree from a specific post ID.
101+ *
102+ * @since 1.3.1
103+ *
104+ * @param int $parent_id The post ID to base the menu off of.
105+ * @param string $post_type The post type to check.
106+ * @param bool $grandchild Whether we are getting first level or second level menu items.
107+ *
108+ * @return array The menu.
109+ */
110+ public function get_menu_tree ( $ parent_id , $ post_type , $ grandchild = false ) {
111+
112+ static $ original_parent_id = 0 ;
113+
114+ if ( ! $ grandchild ) {
115+ $ original_parent_id = $ parent_id ;
116+ }
117+
118+ $ children = array ();
119+
120+ $ pages = get_pages ( array (
121+ 'child_of ' => $ parent_id ,
122+ 'parent ' => -1 ,
123+ 'sort_column ' => 'menu_order ' ,
124+ 'post_type ' => $ post_type ,
125+ ) );
126+
127+ if ( empty ( $ pages ) ) {
128+ return $ children ;
129+ }
130+
131+ $ menu_order = 1 ;
132+
133+ if ( ! $ grandchild ) {
134+ // Add parent.
135+ $ home = get_post ( $ parent_id );
136+ $ home ->post_title = __ ( 'Home ' , 'user-locations ' );
137+ array_unshift ( $ pages , $ home );
138+ }
139+
140+ foreach ( $ pages as $ page ) {
141+
142+ $ child = wp_setup_nav_menu_item ( $ page );
143+
144+ $ child ->db_id = $ page ->ID ;
145+ $ child ->menu_item_parent = ( $ original_parent_id == $ page ->post_parent ) ? 0 : $ page ->post_parent ;
146+ $ child ->menu_order = $ menu_order ;
147+
148+ $ children [] = $ child ;
149+
150+ $ menu_order ++;
151+
152+ }
153+
154+ return $ children ;
155+ }
156+
63157 /**
64158 * Remove post info from location pages
65159 *
0 commit comments