如何使用“Feed JSON”插件修复这些PHP警告?

时间:2011-05-09 作者:user1462

使用WordPress插件时,我收到以下PHP警告”Feed JSON“.有人能告诉我怎么修理吗?”?

警告:为/mypath/wp-content/plugins/feed-json/feed-json模板中的foreach()提供的参数无效。php第39行

警告:无法修改标题信息-标题已由/mypath/wp-content/plugins/feed-json/feed-json-template.php:39中的/mypath/wp-content/plugins/feed-json/feed-json-template发送。php在线53

下面是feed json模板中使用的代码。第39行和第53行在每行周围用**突出显示。

<?php
/**
 * JSON Feed Template for displaying JSON Posts feed.
 *
 */
$callback = trim(esc_html(get_query_var(\'callback\')));
$charset = get_bloginfo(\'charset\');

if ( have_posts() ) {
    $json = array();
    while ( have_posts() ) {
        the_post();
        $id = (int) $post->ID;

        $single = array(
            \'id\'        => $id ,
            \'title\'     => get_the_title() ,
            \'permalink\' => get_permalink(),
            \'content\'   => get_the_content(),
            \'excerpt\'   => get_the_excerpt(),
            \'date\'      => the_date(\'Y-m-d H:i:s\',\'\',\'\',false) ,
            \'author\'    => get_the_author() ,
            );

        // thumbnail
        if (has_post_thumbnail($id)) {
            $single["thumbnail"] = preg_replace("/^.*[\'\\"](https?:\\/\\/[^\'\\"]*)[\'\\"].*/i","$1",get_the_post_thumbnail($id));
        }

        // category
        $categories = array();
        foreach((get_the_category()) as $category) { 
            $categories[] = $category->cat_name; 
        }
        $single["categories"] = $categories;

        // tag
        $tags = array();
        **foreach((get_the_tags()) as $tag) {** 
            $tags[] = $tag->name; 
        }
        $single["tags"] = $tags;

        $json[] = $single;
    }
    $json = json_encode($json);

    nocache_headers();
    if (!empty($callback)) {
        header("Content-Type: application/x-javascript; charset=$charset");
        echo "$callback($json);";
    } else {
        **header("Content-Type: application/json; charset=$charset");**
        echo "$json";
    }

} else {
    header("HTTP/1.0 404 Not Found");
    wp_die("404 Not Found");
}
?>

1 个回复
最合适的回答,由SO网友:MZAweb 整理而成

可能get\\u the\\u tags()返回的是空数组。尝试将功能更改为:

$my_tags = get_the_tags();
if ($my_tags){
    foreach($my_tags as $tag) {
        $tags[] = $tag->name; 
    }
}

结束