如何在wordpress菜单中插入短代码 [英] how to insert shortcode into wordpress menu

查看:54
本文介绍了如何在wordpress菜单中插入短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个代码制作了一个菜单项.菜单项出现,但短代码输出不存在.有什么我可以添加的东西或可以做到这一点的不同方法.我也添加了,希望这可能会有所帮助.

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

谢谢

推荐答案

您不能直接在菜单页面的菜单 URL 中使用短代码,因为括号会被去掉.但是您可以像这样使用占位符:#profile_link#.

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

使用 functions.php 中的以下代码,您可以创建一个带有 URL #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.

/**
 * Filters all menu item URLs for a #placeholder#.
 *
 * @param WP_Post[] $menu_items All of the nave menu items, sorted for display.
 *
 * @return WP_Post[] The menu items with any placeholders properly filled in.
 */
function my_dynamic_menu_items( $menu_items ) {

    // A list of placeholders to replace.
    // You can add more placeholders to the list as needed.
    $placeholders = array(
        '#profile_link#' => array(
            'shortcode' => 'my_shortcode',
            'atts' => array(), // Shortcode attributes.
            'content' => '', // Content for the shortcode.
        ),
    );

    foreach ( $menu_items as $menu_item ) {

        if ( isset( $placeholders[ $menu_item->url ] ) ) {

            global $shortcode_tags;

            $placeholder = $placeholders[ $menu_item->url ];

            if ( isset( $shortcode_tags[ $placeholder['shortcode'] ] ) ) {

                $menu_item->url = call_user_func( 
                    $shortcode_tags[ $placeholder['shortcode'] ]
                    , $placeholder['atts']
                    , $placeholder['content']
                    , $placeholder['shortcode']
                );
            }
        }
    }

    return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );

您只需要在 $placeholders 数组中设置 'shortcode',以及可选的 'atts''content'.

You just need to set 'shortcode' in the $placeholders array, and optionally 'atts' and 'content'.

例如,如果您的简码是这样的:

For example, if your shortcode is like this:

[example id="5" other="test"]Shortcode content[/example]

你会更新:

'#placeholder#' => array(
    'shortcode' => 'example';
    'atts' => array( 'id' => '5', 'other' => 'test' );
    'content' => 'Shortcode content';
),

<小时>

请注意,我 不使用 do_shortcode() 因为它是一个资源密集型功能,在这种情况下不是适合这项工作的工具.


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.

这篇关于如何在wordpress菜单中插入短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆