1

我正在尝试使用 $wp_registered_sidebars 获取所有已注册侧边栏的列表,但它返回一个空数组。

这是我的代码:

foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
    $sidebar_options[$sidebar['id']] = $sidebar['name'];
}

它返回 Array()

在页面模板中有效,但在我的functions.php中无效

任何想法?谢谢

4

2 回答 2

3

把这个函数放在你的主题functions.php中

    function get_my_widgets()
    {
    foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
    {
        $sidebar_options[$sidebar['id']] = $sidebar['name'];
    }
    }

add_action('init','get_my_widgets');

并像往常一样调用此函数get_my_widgets();以获取已注册的侧边栏列表

于 2013-09-05T16:41:11.120 回答
0

在你的主题functions.php中添加以下函数

<?php
function my_sidebar_selectbox( $name = '', $current_value = false ) {
    global $wp_registered_sidebars;
    if ( empty( $wp_registered_sidebars ) )
        return;
     foreach ( $wp_registered_sidebars as $sidebar ) :
     echo $sidebar['name'];
     endforeach; 

}
?>

并调用它,因为my_sidebar_selectbox();它对我有用

于 2013-09-05T17:39:24.303 回答