PHP:不用eval即可解串(在短代码中使用Wordpress nav walker)

时间:2018-08-09 19:10:41

标签: php wordpress menu shortcode wp-nav-walker

我正在使用Wordpress扩展“ Menu Shortcode”的代码,以尝试使其沃克功能正常工作。这是页面中显示的简码:

[listmenu menu=TOC]

(无论值是否用引号引起,这都起作用)

以及插件代码:

<?php

function list_menu($atts, $content = null) {
    extract(shortcode_atts(array(  
        'menu'            => '', 
        'container'       => 'div', 
        'container_class' => '', 
        'container_id'    => '', 
        'menu_class'      => 'menu', 
        'menu_id'         => '',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'before'          => '',
        'after'           => '',
        'link_before'     => '',
        'link_after'      => '',
        'depth'           => 0,
        'walker'          => '',
        'theme_location'  => ''), 
        $atts));

    return wp_nav_menu( array( 
        'menu'            => $menu, 
        'container'       => $container, 
        'container_class' => $container_class, 
        'container_id'    => $container_id, 
        'menu_class'      => $menu_class, 
        'menu_id'         => $menu_id,
        'echo'            => false,
        'fallback_cb'     => $fallback_cb,
        'before'          => $before,
        'after'           => $after,
        'link_before'     => $link_before,
        'link_after'      => $link_after,
        'depth'           => $depth,
        'walker'          => $walker,
        'theme_location'  => $theme_location));
}

//Create the shortcode
add_shortcode("listmenu", "list_menu");
add_filter ('widget_text', 'do_shortcode');

其中一些属性工作得很好(即,我可以在短代码中设置深度,link_before等),但有些不起作用-容器似乎不起作用,并且与我正在处理的内容相关, walker 不起作用。我是否认为代码可能将“ new Extended_Walker()”作为字符串接收并因此无法初始化呢?我尝试使用eval(),但是除了危险之外,它似乎没有用!有没有办法允许我在短代码中设置助行器的选择?

谢谢!

0 个答案:

没有答案