Menu In Post Shortcode

Below, you’ll find a detailed guide on how to add the Menu In Post Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Menu In Post Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Menu In Post Plugin and the shortcodes it provides:

Plugin Icon
Menu In Post

"Menu In Post is a user-friendly WordPress plugin that enables you to easily integrate custom menus directly into your posts. Streamline navigation and enhance user experience with this versatile tool."

★★★★★ (8) Active Installs: 3000+ Tested with: 6.3.2 PHP Version: 7.4
Included Shortcodes:
  • [menu_in_post_menu]

Menu In Post [menu_in_post_menu] Shortcode

The ‘menu_in_post_menu’ shortcode from the Menu-In-Post plugin dynamically generates a menu within a post. It enables customization of menu attributes, including depth, style, and placeholder text. This shortcode also supports two styles: dropdown and list. It uses a unique walker class for each. The ‘append_to_url’ attribute allows appending a string to each menu item URL.

Shortcode: [menu_in_post_menu]

Parameters

Here is a list of all possible menu_in_post_menu shortcode parameters and attributes:

  • menu – The ID of the menu to display in the post.
  • menu_class – The CSS class to apply to the menu.
  • menu_id – The CSS ID to apply to the menu.
  • container – The HTML element to wrap around the menu.
  • container_class – The CSS class to apply to the container.
  • container_id – The CSS ID to apply to the container.
  • style – Determines if the menu displays as a list or dropdown.
  • placeholder_text – The text to display when no option is selected in a dropdown menu.
  • append_to_url – Text to be added to the end of each menu item’s URL.
  • depth – The number of levels deep the menu should go.

Examples and Usage

Basic example – Display a menu by its ID using the ‘menu_in_post_menu’ shortcode.

[menu_in_post_menu menu=3 /]

Advanced examples

Display a menu with a custom depth level. This allows you to control how many levels of a hierarchical menu are displayed.

[menu_in_post_menu menu=3 depth=2 /]

Display a menu as a dropdown with a custom placeholder text and a custom ID.

[menu_in_post_menu menu=3 style="dropdown" placeholder_text="Select a page..." menu_id="my_custom_menu" /]

Display a menu with a custom string appended to the URL of each menu item. This can be useful for tracking clicks on menu items, for example.

[menu_in_post_menu menu=3 append_to_url="?source=menu_in_post" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [menu_in_post_menu] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('menu_in_post_menu', 'outputMenuInPostMenu');

Shortcode PHP function:

function outputMenuInPostMenu($atts = array())
{
    if (isset($atts['menu'])) {
        $menu = absint($atts['menu']);
    } else {
        $menu = 0;
    }
    if ($menu == 0) {
        return fallbackMenuInPost();
    } else {
        $args = array(
            'menu'=>$menu, 
            'fallback_cb'=>'fallbackMenuInPost', 
            'echo'=>false
        );
    }
    /* 
        If menu_id is empty, don't pass a value, and the menu slug with an 
        incremented value added will be used. 
         
        If container_class is empty, don't pass a value and 
        'menu-{menu slug}-container' will be used.
    */
    $defaults = array(
        'menu_class'=>'menu',
        'menu_id'=>'',    
        'container'=>'div', 
        'container_class'=>'', 
        'container_id'=>'', 
        'style'=>'list', 
        'placeholder_text'=>esc_html(__('Select...', 'menu-in-post')), 
        'append_to_url'=>'', 
        'depth'=>0 
    );
    foreach ($defaults as $att=>$default) {
        switch($att) {
        case 'depth':
            if (isset($atts[$att])) {
                 $passed_depth = absint($atts[$att]);
                if ($passed_depth > 0) {
                    $args['depth'] = $passed_depth;
                }
            } else {
                $atts['depth'] = $default;
            }
            break;
        // These should be only strings.
        default:
            if (isset($atts[$att])) {
                $passed_att = sanitize_text_field($atts[$att]);
                if ($passed_att != '') {
                    $args[$att] = $passed_att;
                }
            } else {
                $atts[$att] = $default;
            }
        }    
    }
    if ($atts['style'] == 'dropdown') {
        $select = '<select class="mip-drop-nav"';
        if ($atts['menu_id'] != '') {
            $select .= ' id="' . $args['menu_id'] . '"';
        }
        $select .= '>';
        $args['items_wrap'] = $select . '<option value="#">' . 
            $atts['placeholder_text'] . '</option>%3$s</select>';
        $args['walker'] = new MIPWalkerNavMenuDropdownBuilder();
    } else {
        if ($atts['append_to_url'] != '') {
            $args['walker'] = new MIPWalkerNavMenuListBuilder();
        } 
    }
    if ($atts['append_to_url'] != '') {
        $args['append_to_url'] = $atts['append_to_url'];
    }
    return wp_nav_menu($args);
}

Code file location:

menu-in-post/menu-in-post/menu-in-post.php

Conclusion

Now that you’ve learned how to embed the Menu In Post Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *