2013-09-05 525 views
1

我想使用$ wp_registered_sidebars獲得所有註冊側邊欄列表,但它返回一個空數組。WordPress的 - 側邊欄列表

這裏是我的代碼:

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

它返回陣列()

在它的工作原理頁面模板,但不是在我的functions.php

任何想法? 感謝

回答

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();獲得註冊的側邊欄列表

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();這是工作給我