0

好的,我在 wordpress 中安装了一个主题,它返回一些错误。

Warning: Invalid argument supplied for foreach() in /home/mvprop/public_html/wp-content/themes/yoo_vox_wp/warp/systems/wordpress.3.0/helpers/system.php on line 339

这个问题在这些论坛上得到解决

但我不明白最后一篇文章是关于什么的。这家伙发布了一堆解决问题的随机代码。他没有具体说明它来自哪里或放在哪里。只是粘贴似乎与任何事情无关的代码。

第 334 行到文件末尾

function getWidgets($position = null) {

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

    if (!is_null($position)) {
        return isset($this->widgets[$position]) ? $this->widgets[$position] : array();
    }

    return $this->widgets;
}

/*
    Function: displayWidget
        Checks if a widget should be displayed

    Returns:
        Boolean
*/
function displayWidget($widget) {
    if (!isset($widget->options['display']) || in_array('*', $widget->options['display'])) return true;

    foreach ($this->getQuery() as $q) {
        if (in_array($q, $widget->options['display'])) {
            return true;
        }
    }

    return false;
}

/*
    Function: overrideConfig
        Overrides default config based on page

    Returns:
        Void
*/
function overrideConfig() {
    if (!count($this->config_overrides)) return;

    foreach ($this->getQuery() as $q) {
        if (isset($this->config_overrides[$q])) {
            $this->warp->config->parseString($this->config_overrides[$q]);
        }
    }
}

/*
    Function: isBlog

    Returns:
        Boolean
*/
function isBlog() {
    return true;
}

/*
    Function: isPreview
        Checks for default widgets in theme preview 

    Returns:
        Boolean
*/
function isPreview($position) {

    // preview postions
    $positions = array('logo', 'right');

    return is_preview() && in_array($position, $positions);
}

/*
    Function: ajaxSearch
        Ajax search callback

    Returns:
        String
*/
function ajaxSearch(){
    global $wp_query;

    $result = array('results' => array());
    $query  = isset($_REQUEST['s']) ? $_REQUEST['s']:"";

    if (strlen($query)>=3) {

        $wp_query->query_vars['s'] = $query;
        $wp_query->is_search = true;

        foreach ($wp_query->get_posts() as $post) {

            $content = !empty($post->post_excerpt) ? strip_tags(do_shortcode($post->post_excerpt)) : strip_tags(do_shortcode($post->post_content));

            if (strlen($content) > 255) {
                $content = substr($content, 0, 254).'...';
            }

            $result['results'][] = array(
                'title' => $post->post_title,
                'text'  => $content,
                'url'   => get_permalink($post->ID)
            );
        }
    }

    die(json_encode($result));
}

/*
    Function: _adminInit
        Admin init actions

    Returns:
        Void
*/
function _adminInit() {

    if ((defined('DOING_AJAX') && DOING_AJAX) && isset($_POST['warp-ajax-save'])) {

        // update option values
        foreach ($_POST as $option => $value) {
            if (preg_match('/^(warp_|'.preg_quote($this->prefix, '/').')/', $option)) {
                update_option($option, $value);
            }
        }

        die();
    }

    wp_enqueue_script('warp-admin', rtrim(get_bloginfo('template_url'),'/').'/warp/systems/wordpress.3.0/js/wp-admin.js', false, '1.0');
    add_action('wp_ajax_save_nav_settings', array($this,'_save_nav_settings'));
    add_action('wp_ajax_get_nav_settings', array($this,'_get_nav_settings'));
}

/*
    Function: _adminHead
        Admin head actions

    Returns:
        Void
*/
function _adminHead() {

    // init vars
    $path =& $this->getHelper('path'); 

    $head[] = '<link rel="stylesheet" href="'.$path->url('warp:systems/wordpress.3.0/css/admin.css').'" type="text/css" />';
    $head[] = '<script type="text/javascript" src="'.$path->url('warp:systems/wordpress.3.0/js/admin.js').'"></script>';

    echo implode("\n", $head);
}

/*
    Function: _adminMenu
        Admin menu actions

    Returns:
        Void
*/
function _adminMenu() {

    // init vars
    $path =& $this->getHelper('path');
    $name = $this->xml->document->getElement('name');
    $icon = $path->url('warp:systems/wordpress.3.0/images/yoo_icon_16.png');

    if (function_exists('add_object_page')) {
        add_object_page('', $name->data(), 8, 'warp', false, $icon);
    } else {
        add_menu_page('', $name->data(), 8, 'warp', false, $icon); 
    }

    add_submenu_page('warp', 'Theme Options', 'Theme Options', 8, 'warp', array($this, '_adminThemeOptions'));
    add_submenu_page('warp', 'Widget Options', 'Widget Options', 8, 'warp_widget', array($this, '_adminWidgetOptions'));
}

/*
    Function: _adminThemeOptions
        Render admin theme options layout

    Returns:
        Void
*/  
function _adminThemeOptions() {

    // init vars
    $path  =& $this->getHelper('path');
    $xml   =& $this->getHelper('xml');
    $http  =& $this->getHelper('http');
    $check =& $this->getHelper('checksum');

    // get warp xml
    $warp_xml = $xml->load($path->path('warp:warp.xml'), 'xml', true);

    // update check
    $update = null;
    if ($url = $warp_xml->document->getElement('updateUrl')) {

        // get template info
        $template = get_template();
        $version  = $this->xml->document->getElement('version');
        $url      = sprintf('%s?application=%s&version=%s&format=raw', $url->data(), $template, $version->data());

        // only check once a day 
        if (get_option($this->prefix.'update_check') != date('Y-m-d').' '.$version->data()) {
            if ($request = $http->get($url)) {
                update_option($this->prefix.'update_check', date('Y-m-d').' '.$version->data());
                update_option($this->prefix.'update_data', $request['body']);
            }
        }

        // decode update response
        $update = json_decode(get_option($this->prefix.'update_data'));
    }

    // verify theme files
    if (($checksums = $path->path('template:checksums')) && filesize($checksums)) {
        $check->verify($path->path('template:'), $log);
    } else {
        $log = false;
    }

    echo $this->warp->template->render('admin/theme_options', array('xml' => $this->xml, 'warp_xml' => $warp_xml, 'update' => $update, 'checklog' => $log));
}

/*
    Function: _adminWidgetOptions
        Render admin widget options layout

    Returns:
        Void
*/  
function _adminWidgetOptions() {

    // get position settings
    $position_settings = $this->warp->config->get('warp.positions');

    // get module settings
    $module_settings = array();
    $settings = $this->xml->document->getElement('modulesettings');

    foreach ($settings->children() as $setting) {
        $module_settings[$setting->attributes('name')] = $setting;
    }

    echo $this->warp->template->render('admin/widget_options', compact('position_settings', 'module_settings'));
}

/*
    Function: getMenuItemOptions
        Retrieve menu by id

    Parameters:
        $id - Menu Item ID

    Returns:
        Array
*/
function getMenuItemOptions($id) {

    $menu_settings = array(
        'columns'     => 1,
        'columnwidth' => -1,
        'image'       => ''
    );

    if (isset($this->menu_item_options[$id])) {
        $menu_settings = array_merge($menu_settings, $this->menu_item_options[$id]);
    }

    return $menu_settings;
}


/*
    Function: _save_nav_settings
        Saves menu item settings

    Returns:
        Void
*/  
function _save_nav_settings() {

    if (isset($_POST['menu-item'])) {

        $menu_item_settings = $this->menu_item_options;

        foreach ($_POST['menu-item'] as $itemId=>$settings){
            $menu_item_settings[$itemId] = $settings;
        }

        update_option($this->prefix.'menu-items', $menu_item_settings);
        $this->menu_item_options = $menu_item_settings;
    }

    die();
}

/*
    Function: _get_nav_settings
        Returns menu item settings as json

    Returns:
        Boolean
*/
function _get_nav_settings() {
    die(json_encode($this->menu_item_options));
    }

}

    /*
        Function: mb_strpos
            mb_strpos function for servers not using the multibyte string extension
    */
    if (!function_exists('mb_strpos')) {
        function mb_strpos($haystack, $needle, $offset = 0) {
            return strpos($haystack, $needle, $offset);
        }
    }
4

1 回答 1

1

基本上,这条警告消息告诉您的是,您传递给 foreach 的变量不是数组或对象。通过测试变量( is_array($var) 或 is_object($var) )或将此代码块放在 try-catch 中,确保您的变量有效。

如果 $var 应该是一个数组,你也应该初始化它以确定。

$var = Array();
.
. // code that may change the data type of $var
.
if (is_array($var)) {
    foreach($var as $v) {
        //code here
    }
}

来自http://php.net/manual/en/control-structures.foreach.php的手册:

foreach 构造提供了一种迭代数组的简单方法。foreach 仅适用于数组和对象,当您尝试在具有不同数据类型的变量或未初始化的变量上使用它时会发出错误。

于 2012-07-25T15:42:41.607 回答