使用wordpress时出现foreach()错误wp_get_sidebars_widgets()

时间:2012-09-25 21:15:27

标签: php wordpress foreach

function getWidgets($position = null) {
    if (empty($this->widgets)) {
        foreach (wp_get_sidebars_widgets() as $pos => $ids) {
            $this->widgets[$pos] = array();
            foreach ($ids as $id) {                  // error is here
                $this->widgets[$pos][$id] = $this->getWidget($id);
            }
        }
    }
}

这些是第305-314行。

我收到了这个错误:

" Warning: Invalid argument supplied for foreach() in /home/content/73/9889573/html/wp-content/themes/yoo_spark_wp/warp/systems/wordpress.3.0/helpers/system.php on line 310 " 

有人可以告诉我如何解决它

1 个答案:

答案 0 :(得分:2)

wp_get_sidebars_widgets()返回一维数组。

参考http://codex.wordpress.org/Function_Reference/wp_get_sidebars_widgets

$ids不是数组。您无法在foreach循环中遍历它。

试试这个:

$widgets = array();
foreach (wp_get_sidebars_widgets() as $pos => $id) {
    $widgets[$pos] = $this->getWidget($id);
}