0

I'm getting the following PHP warnings when using the WordPress plug-in "Feed JSON". Can someone tell me how to fix them?

Warning: Invalid argument supplied for foreach() in /mypath/wp-content/plugins/feed-json/feed-json-template.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /mypath/wp-content/plugins/feed-json/feed-json-template.php:39) in /mypath/wp-content/plugins/feed-json/feed-json-template.php on line 53

Here is the code used in feed-json-template. Lines 39 and 53 are highlighted with ** around each line.

<?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 Answer 1

1

Probably get_the_tags() is returning an empty array. Try changing the function to:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.