如何将短代码插入wordpress菜单

xyz*_*xyz 8 wordpress menu add-filter shortcode

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

add_filter('wp_nav_items', 'do_shortcode', 7);
Run Code Online (Sandbox Code Playgroud)

或者也许有人知道这是不可能的,可以告诉我.

/* 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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

usa*_*man 21

@Tim这段代码可行

把它放在functions.php文件中

add_filter('wp_nav_menu_items', 'do_shortcode');
Run Code Online (Sandbox Code Playgroud)

  • @JohnSmith 这完全是什么? (4认同)

J.D*_*.D. 9

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

使用以下代码functions.php,您可以使用URL创建自定义菜单项#profile_link#,它将用您的短代码替换它.

/**
 * 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' );
Run Code Online (Sandbox Code Playgroud)

你只需要设置'shortcode'的在$placeholders阵列,以及可选'atts''content'.

例如,如果您的短代码是这样的:

[example id="5" other="test"]Shortcode content[/example]
Run Code Online (Sandbox Code Playgroud)

你会更新:

'#placeholder#' => array(
    'shortcode' => 'example';
    'atts' => array( 'id' => '5', 'other' => 'test' );
    'content' => 'Shortcode content';
),
Run Code Online (Sandbox Code Playgroud)

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

  • @JD 感谢您的回答,我的简码是 [easy-pricing-table id="4227"],我将您的代码更改为 $shortcode = 'easy-pricing-table'; $atts = array('id' =&gt; 4227); 我创建了一些自定义链接,其 URL 是 #profile_link# 用于测试,但它不起作用。没有任何错误或警告,但没有任何反应 (2认同)