Site logo

[Support request] menu-specific hook

Home Forums Support [Support request] menu-specific hook

Home Forums Support menu-specific hook

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #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>
    #1699703
    Elvin
    Staff
    Customer Support

    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;
    }
    #1700025
    onalti

    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>
    #1700504
    David
    Staff
    Customer Support

    Hi there,

    maybe this stack answer will help:

    https://wordpress.stackexchange.com/a/242772

    #1700902
    onalti

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

    #1700907
    onalti

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

    #1700925
    Leo
    Staff
    Customer Support

    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.

    #1701156
    onalti

    hooks is not related to the theme?

    #1701475
    David
    Staff
    Customer Support

    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>

    #1703213
    onalti

    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>

    #1703654
    David
    Staff
    Customer Support

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

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.