1

I'm needing a little help getting this figured out. I have a query that's not quite working right now, but I need it to create a custom feed that includes category information and then includes the featured image from the first post in that category.

Anyone have thoughts on how to get that running?

/**
 * Custom Feed for Category Listing
 */
    function outputXMLFeed()
    {
        echo '<?xml version="1.0"?>';
        echo '<items>';
        $args=array(
          'orderby' => 'name',
          'order' => 'ASC'
        );
        $categories=get_categories($args);
        $posts = get_posts(array('category' => $category->term_id));
        foreach($categories as $category) { 
            echo '<item>';
            echo '<catID>' . $category->term_id . '</catID>';
            echo '<catname>' . $category->name . '</catname>';
            echo '<postcount>' . $category->category_count . '</postcount>';
            echo '<slug>' . $category->slug . '</slug>';
            echo '<featured>' . [VARIABLE HERE] . '</featured>';
            echo '</item>';
        }
        echo '</items>';
    }
    add_action('init', 'add_my_feed');

    function add_my_feed(  ) {
      add_feed("myFeed", "outputXMLFeed");
    }
0

First define a function to return the first post of a category by a given category ID ex:

function get_category_post($cat_id){
    $post_args = array(
        'numberposts' => 1,
        'category' => $cat_id,
        'fields' => 'ids'
    );
    $posts = get_posts($post_args);
    return $posts[0];
}

Then, once you have that function you can use it like this:

function outputXMLFeed()
{
    echo '<?xml version="1.0"?>';
    echo '<items>';
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $categories=get_categories($args);
    foreach($categories as $category) { 
        echo '<item>';
        echo '<catID>' . $category->term_id . '</catID>';
        echo '<catname>' . $category->name . '</catname>';
        echo '<postcount>' . $category->category_count . '</postcount>';
        echo '<slug>' . $category->slug . '</slug>';
        echo '<featured>' . get_the_post_thumbnail(get_category_post($category->term_id) , $size = 'post-thumbnail') . '</featured>';
        echo '</item>';
    }
    echo '</items>';
}
add_action('init', 'add_my_feed');

function add_my_feed(  ) {
  add_feed("myFeed", "outputXMLFeed");
}
  • Ok, cool. I've got all that put in place, but for some reason it's still not outputting all the individual items within the <items></items> tag. Thoughts? – Jake Lisby Jan 1 '13 at 17:16
  • working just fine on my end gist.github.com/4428853 – Bainternet Jan 1 '13 at 17:39
  • Can this only run in a plugin? I've been trying to set it up through the functions.php – Jake Lisby Jan 2 '13 at 13:59
  • No, it should run from functions.php as well – Bainternet Jan 2 '13 at 14:23
  • Strange, this is the only output I'm getting: sandboxdewsly.com/?feed=myFeed. This is on a multi-site setup, should that have any affect on it? – Jake Lisby Jan 2 '13 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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