I have made a menu item with this code.
The menu item shows up but the shortcode output is not there.
Is there something I can add or a different method that will do this.
I have added also in hopes this might help.
add_filter('wp_nav_items', 'do_shortcode', 7);
Or maybe someone knows this is not possible and can tell me.
/* Nav Menu */
function add_profile_link_to_nav(){
if ( is_user_logged_in() ) { ?>
<ul>
<li class="menu-item"id="one"> <a href="http://example.com/members/">All Members</a>
<ul class="sub-menu">
<li class="menu-item"><?php echo custom_execute_shortcode(); ?> </li>
</ul>
</li>
</ul> <!--end menu--->
<?php }
}
add_action( "wp_nav_items","add_profile_link_to_nav" );
function custom_execute_shortcode() {
$myfunction= '[my shortcode"]';
$myfunction_parsed = do_shortcode($myfunction);
return $myfunction_parsed;
}
Thanks
@Tim
This code will work
put it in functions.php file
You can’t use shortcodes directly in the menu URL on the menu page, because the brackets get stripped out. But you can use placeholders like this:
#profile_link#
.With the following code in
functions.php
, you can create a custom menu item with the URL#profile_link#
, and it will replace that with your shortcode.You just need to set
'shortcode'
in the$placeholders
array, and optionally'atts'
and'content'
.For example, if your shortcode is like this:
You would update:
Note that I don’t use
do_shortcode()
because it is a resource intensive function and isn’t the right tool for the job in this case.Enable description on the menu page, paste to the description textarea of the link your shortcode, in functions.php add next code: