使用短代码的Wordpress PHP动态菜单项

时间:2015-12-28 22:09:56

标签: php regex wordpress shortcode

这是我的菜单项结构:

enter image description here

以下是用于创建短代码以添加动态菜单项的代码:

function my_dynamic_menu_items($menu_items)
{
foreach ($menu_items as $menu_item) {
    if ('#short_code#' === $menu_item->url) {

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


function my_custom_menu_item($items, $args)
{
preg_match_all('#<li[^"]*"menu-item-([^"]*)" class="shortcode ([^"]*)"><a[^"]*"[^"]*" href="([^"]*)">[^<]*</a></li>#', $items, $x);
if (count($x[0])) {
    foreach ($x[0] as $key => $value) {
        $items = str_replace($value,'<li id="menu-item-'.$x[1][$key].'" class="shortcode '.$x[2][$key].'">'.do_shortcode("[".str_replace("#","",$x[3][$key])."]").'</li>',$items);
    }
}
 return $items;
}
add_filter('wp_nav_menu_items', 'my_custom_menu_item', 10, 2);


function show_places_func($atts)
{
global $my_category_slug;
return "<span><a href='/".$my_category_slug."/local/impacts'>PLACES</a></span>";
}
add_shortcode( 'show_places', 'show_places_func' );

问题是只适用于单个菜单项(没有子/子项)。如何使父项和子项动态化?提前谢谢!

0 个答案:

没有答案