- This topic has 10 replies, 4 voices, and was last updated 4 years, 5 months ago by
David.
-
AuthorPosts
-
March 17, 2021 at 6:17 pm #1699646
onalti
with wp_nav_menu_items filter I want to add a special hook into primary-menu, after . it shows but not under .
I do not know how to do it. default hooks are not suitable where I want them.add_filter( 'wp_nav_menu_items', 'prefix_add_div', 10, 2 ); function prefix_add_div( $items, $args ) { $items = '' . $items . do_action( 'generate_custom_primary_menu' ); return $items; } <div class="main-nav"> <ul id="menu-main" class="main-menu menu sf-menu" aria-hidden="false"> <li></li> <li><ul><li></li></ul> <li></li> </ul> <div class='custom-hook'> the area where I want my hook to work</div> <div>
March 17, 2021 at 8:59 pm #1699703Elvin
StaffCustomer SupportHi there,
Try this instead:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); }); add_filter( 'wp_nav_menu_items', 'prefix_add_div', 15, 2 ); function prefix_add_div( $items, $args ) { $items .= '<li class="menu-item">'.do_shortcode( '[portable_hook hook_name="generate_custom_primary_menu"]' ).'</li>'; return $items; }
March 18, 2021 at 3:34 am #1700025onalti
thank you for your reply. This didn’t solve my problem. I want it to be outside the
<ul>
element. This added as the<li>
element in the helix.<div id="primary-menu" class="main-nav"> <ul id="menu-main" class="main-menu menu sf-menu" aria-hidden="false"> <li> </li> </ul> <div class='custom-hook'> the area where I want my hook to work</div> <div>
March 18, 2021 at 7:14 am #1700504David
StaffCustomer SupportMarch 18, 2021 at 12:23 pm #1700902onalti
I haven’t reached a solution yet. Your solution is the closest. I want it to show after
<ul>
. Thank you.March 18, 2021 at 12:27 pm #1700907onalti
Hi there, when I try to do it with hook. It does not display in the desired area.
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s <div class="foo">bar</div> </ul>'
March 18, 2021 at 12:49 pm #1700925Leo
StaffCustomer SupportHave you tried posting your question in a general WordPress forum like Stackoverflow as David linked above?
It’s not a theme related question so don’t think we can be of more help here:
https://generatepress.com/what-support-includes/Thanks for your understanding.
March 18, 2021 at 5:24 pm #1701156onalti
hooks is not related to the theme?
March 19, 2021 at 2:37 am #1701475David
StaffCustomer SupportThats a WP Core hook not a theme hook – try changing your HTML string from:
items_wrap' => '<ul id="%1$s" class="%2$s">%3$s <div class="foo">bar</div> </ul>
to:
items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul><div class="foo">bar</div>
March 20, 2021 at 12:04 pm #1703213onalti
It’s okay when I add it this way. It is listing in a different area when I add it with hook. When I added it with a hook, it confused me that it was not listed in the right area.
items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul><div class="foo">bar</div>
March 21, 2021 at 5:15 am #1703654David
StaffCustomer SupportCan you share the final code you’re trying with the Hook?
-
AuthorPosts
- You must be logged in to reply to this topic.