Wordpress主题致命错误"致命错误:调用未定义的方法WP_Error :: get_items()"

时间:2017-08-30 07:06:43

标签: php wordpress wordpress-theming

我的一个Wordpress主题显示以下错误:

Fatal error: Call to undefined method WP_Error::get_items() in /home1/mywebsite/public_html/learnphp123.com/wp-content/themes/econews/admin/IDE_admin.php on line 88

该文件的代码是(您可以在第88行看到" get_items()" ....实际上是代码中的第三行):

    if($feed) {
                $html = '<ul>';
                foreach ($feed->get_items() as $item){
                    $html.="<li><span>".$item->get_date('d M Y')."</span> <a href=\"".$item->get_permalink()."\">".$item->get_title()."</a></li>";
                }    

有人可以告诉我在"get_items()"的位置使用什么吗?

非常感谢你的帮助! 下面给出了该文件的总代码,我只得到第88行与feed get_items()相关的错误。

    <?php

/*
    Copyright 2010, idesigneco.com
    http://idesigneco.com
*/

define('IDE_ADMIN_FEED', 'http://idesigneco.com/wp_pub.php?id=ide_admin_newsfeed.xml&theme='.IDE_CODE.'&ver='.IDE_VERSION);

// load form generator and validator
include IDE_ADMIN_PATH.'IDE_FormGenerator.class.php';

// initialize the admin form
$Form = new IDE_FormGenerator(array(
    'name' => 'admin',
    'method' => 'post',
    'action' => ''
));

// setup the form fields
$Form->elements(
    null,
    array(
        'admin_action' => array(
            'type' => 'hidden', 'value' => 'admin',
            'error' => 'Your settings have been updated.'
        )
    )
);

// load theme specific options
include IDE_ADMIN_PATH.'/IDE_admin_options.php';

$Form->elements(
    null,
    array(
        'submit' => array(
            'type' => 'submit', 'value' => 'Save', 'label' => ''
        ),
    )
);

$Form->printErrors(true);


// ________________________________________________________

// process the form data
if(!empty($_POST['admin_action'])) {

    // unchecked checkbox options don't show up, so fill them with predefined key names
    ide_save_options( array_merge( array_fill_keys($IDE_checkboxes, ''),
                                   stripslashes_deep($_POST)
                                  )
                    );
    $Form->setErrors(array('admin_action'));
}

// ________________________________________________________

// populate the form with saved options
$Form->data($IDE_options);

// ________________________________________________________


// print out the admin area
ide_admin_header();
$Form->generate();
ide_admin_footer();

// ________________________________________________________

// idesigneco news feed
function ide_admin_news() {

    $time = ide_option('admin_newsfeed_time');
    $html =  ide_option('admin_newsfeed');


    // feed expired, update
    if(!$html || !$time || ($time+(5*3600)) < time() ) {
        if(function_exists('fetch_feed')){
            $feed = fetch_feed(IDE_ADMIN_FEED);

            if($feed) {
                $html = '<ul>';
                foreach ($feed->get_items() as $item){
                    $html.="<li><span>".$item->get_date('d M Y')."</span> <a href=\"".$item->get_permalink()."\">".$item->get_title()."</a></li>";
                }
                $html.= '</ul>';
            } else {
                $html = 'Updates unavailable at this time';
            }
        } else {
            // deprecated feed api
            if(function_exists('fetch_rss')){
                include_once(ABSPATH . WPINC . '/rss.php');
                $rss = fetch_rss(IDE_ADMIN_FEED);
                $html = '<ul>';
                foreach ($rss->items as $item){
                    $html.="<li><span>".date('d M Y', strtotime($item['pubdate']))."</span> <a href=\"".$item['link']."\">".$item['title']."</a></li>";
                }
                $html.= '</ul>';
            }
        }

        ide_save_option('admin_newsfeed_time', time());
        ide_save_option('admin_newsfeed', $html);
    }


    echo $html;
}


// admin header
function ide_admin_header() {
    ?>
    <div id="ide_admin">
        <div class="left">
        <h1 class="ide_title"><?php echo IDE_NAME.' '.IDE_VERSION; ?></h1>
        <div class="clear"> </div>
    <?php
}

// admin footer
function ide_admin_footer() {
    ?>
        </div><!-- left //-->

        <div class="right">
            <div class="idesigneco">
                <a href="http://idesigneco.com"><img src="<?php echo IDE_ADMIN_STATIC.'/idesigneco.png'; ?>" alt="Theme by iDesignEco" title="Theme by iDesignEco" /></a>
            </div>
            <div class="news">
                <h2>iDesignEco Updates</h2>
                <?php ide_admin_news(); ?>
            </div>
        </div><!-- right //-->
        <div class="clear"> </div>

    </div><!-- ide_admin //-->
    <?php
}

&GT;

1 个答案:

答案 0 :(得分:1)

当获取$feed的数据失败时,可能会发生这种情况,因此它变为没有get_items()方法的WP_Error类。尝试修改代码如下:

if (! is_wp_error( $feed ))  {
            $html = '<ul>';
            foreach ($feed->get_items() as $item){
                $html.="<li><span>".$item->get_date('d M Y')."</span> <a href=\"".$item->get_permalink()."\">".$item->get_title()."</a></li>";
            } 

<强>更新

就像我假设的那样,$feedfetch_feed()获取数据,因此解决方案应该仍然是替换:

 if($feed) {

with:

if (! is_wp_error( $feed ))  {
相关问题