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)
您不能直接在菜单页面的菜单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()它,因为它是一个资源密集型功能,在这种情况下不适合作业.
| 归档时间: |
|
| 查看次数: |
30423 次 |
| 最近记录: |