Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm got an issue here that's stumping me. I'm trying to create a custom RSS feed using add_feed(). The feed seems to have been created successfully, but I'm quite stuck on some bug in the feed template I'm writing.

For some reason, in my code, the_post() seems to return nothing inside the loop. (I've verified this by using print_r). I just don't have access to the typical post-related information that I'd expect.

Even stranger: get_defined_vars() outputs nothing inside the template as well. I wrote up a test script to hosted on another vhost on the same physical machine to verify that PHP is configured correctly, and it seems to be.

Stranger still: despite all of the above, my <item>s aren't coming through blank. Some of the wordpress functions seem to work - just not all of them :/

As a reference, here's the entire template file I'm working on. It's very minimal, but based off of the wp-includes/feed-rss2.php file:

View it on pastebin. (Sorry, tried to put it here, but the formatter was getting pretty confused by it.)

Any help is tremendously appreciated. I'm stumped!

share|improve this question
add comment

2 Answers

OK. So I think I figured this one out myself. I'll document it here for the sake of those who may fall into the same trap.

I've been using a template file to structure my feed. In my functions.php file, this is what the relevant portions of code looked like before:

add_feed('weekly_deals', 'weekly_deals_create_feed');
function weekly_deals_create_feed(){
    # for the sake of cleanliness, simply include a template file here
    include get_template_directory() . "/weekly-deals-rss.php";
}

Via random Googling, I discovered to do this instead:

add_feed('weekly_deals', 'weekly_deals_create_feed');
function weekly_deals_create_feed(){
    # for the sake of cleanliness, simply include a template file here
    load_template(get_template_directory() . "/weekly-deals-rss.php");
}

The latter works, while the former simply doesn't. Apparently Wordpress somehow destroys (or otherwise makes unavailable) all of the environment data when using include for templates like I did above.

Live and learn :/

share|improve this answer
add comment

in my code, the_post() seems to return nothing inside the loop

It is not supposed to return anything. It just sets up global variables with info about current post and iterates the loop. Check global $post variable for current post instead.

Even stranger: get_defined_vars() outputs nothing inside the template as well.

That function outputs variable from current scope. Since your templates is loaded inside of some feed-related function it doesn't output global variables because they are out of scope for that function.

share|improve this answer
    
Thanks, @Rarst. I ended up solving this one yesterday afternoon, but couldn't post the solution to the forum until now because I'm a new member (and new members must wait before answering their own questions.) Sure enough, it did end up being a scoping issue, like you said. Thanks! (I'd upvote you now, but I don't have the necessary reputation.) –  chrisallenlane Apr 27 '11 at 14:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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