我用这段代码制作了一个菜单项。 菜单项显示,但短代码输出不存在。 有什么我可以添加的东西或者不同的方法可以做到这一点。 我也添加了希望这可能有所帮助。
add_filter('wp_nav_items', 'do_shortcode', 7);
或者也许有人知道这是不可能的并且可以告诉我。
/* 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;
}
谢谢
@蒂姆 这段代码可以工作
将其放入functions.php文件中
add_filter('wp_nav_menu_items', 'do_shortcode');
您不能直接在菜单页面的菜单 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' );
您只需在
'shortcode'
数组中设置 $placeholders
,以及可选的 'atts'
和 'content'
。
例如,如果您的简码是这样的:
[example id="5" other="test"]Shortcode content[/example]
您将更新:
'#placeholder#' => array(
'shortcode' => 'example';
'atts' => array( 'id' => '5', 'other' => 'test' );
'content' => 'Shortcode content';
),
请注意,我不使用
do_shortcode()
,因为它是一个资源密集型功能,在这种情况下不是适合该工作的工具。
在菜单页面上启用描述,将您的短代码粘贴到链接的描述文本区域,在functions.php中添加以下代码:
add_filter('walker_nav_menu_start_el', function($item_output, $item) {
if (!is_object($item) || !isset($item->object)) {
return $item_output;
}
if ($item->ID === 829) {
$item_output = do_shortcode($item->description);
}
return $item_output;
}, 20, 2);
打开functions.php并插入提供的代码:
// Custom Walker Function
class Shortcode_Menu_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth = 0, $args = NULL, $id = 0) {
$output .= apply_filters('walker_nav_menu_start_el', do_shortcode($item->title), $item, $depth, $args);
}
}
// Apply Custom Walker to Menu
add_filter('wp_nav_menu_args', 'custom_menu_walker');
function custom_menu_walker($args) {
if (isset($args['walker']) && $args['walker'] === false) {
$args['walker'] = new Shortcode_Menu_Walker();
}
return $args;
}