无法使用索引var调用函数

时间:2016-09-12 16:41:12

标签: php wordpress function variables widget

为什么我不能使用循环中的var $ i来调用函数?

for ($i=1; $i <= 10; $i++){
 $widget_id = $i;               
 $sidebar_id = 'sidebar-ourfocus';
 $widget_type = 'ctup-ads-widget';
 $widclass = get_sidebar_widget_options($sidebar_id, $widget_type, $widget_id);
}

如果我为widget_id使用固定值,则没有问题

$widclass = get_sidebar_widget_options($sidebar_id, $widget_type, '1');

function get_sidebar_widget_options($sidebar_id, $widget_type, $widget_id)
{
    // Grab the list of sidebars and their widgets
    $sidebars = wp_get_sidebars_widgets();
    // Just grab the widgets for our sidebar
    $widgets = $sidebars[$sidebar_id];


    // If we didn't find the given widget in the given sidebar, throw an error
    if ( !$widget_id )
        throw new Exception("Widget not found in sidebar");

    // Grab the options of each instance of our $widget_type from the DB
    $options = get_option('widget_' . $widget_type);

    // Ensure there are settings to return
    if ( !isset($options[$widget_id]) )
        throw new Exception("Widget has no saved options");

    // Grab the settings
    $widget_options = $options[$widget_id];

    return $widget_options;

我还尝试将$ i转换为String,如

$widget_id = (string) $i;

但它没有帮助:/

0 个答案:

没有答案