Site logo

Archived topic

menu-specific hook

10 replies · Started by onalti on March 17, 2021

Viewing posts 1–11 of 11

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>

Hi 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;
}

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>

I haven't reached a solution yet. Your solution is the closest. I want it to show after <ul>. Thank you.

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>'

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

hooks is not related to the theme?

Thats 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>

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>

Can you share the final code you're trying with the Hook?

This archived topic is closed to new replies.