Opened 8 months ago
Closed 7 months ago
#22031 closed enhancement (duplicate)
Conditional tags don't work within add_feed() callback function
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Feeds | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | robert@… |
Description
I was putting together a custom feed and wanted to pick a different template based on the context of where I was in the site. So for example I wanted one feed template on the home/front page and another for category pages.
Using add_feed() and browsing to that feed results in only is_feed() being true and not any of the other conditional tags.
I was wondering if a) there's a reason for that and b) if it's something we could fix.
I'm not entirely sure where to look though. Attempts at running $wp_query->parse_query_vars() to force the conditionals to be set were unsuccessful.
The following code is a quick demo:
add_feed( 'new_feed', 'feed_output' );
function feed_output() {
if ( is_home() )
echo 'home feed';
if ( is_singular() )
echo 'singular feed';
if ( is_category() )
echo 'category feed';
}
Nothing will be output on /feed/new_feed/ URLs even if on home, single or category page.
Change History (4)
comment:1
sanchothefat — 8 months ago
- Cc robert@… added
comment:3
sanchothefat — 8 months ago
@ocean90 I get what you're saying. Thing is though feeds change depending on context eg. /category/cat-name/feed/ is a feed for that category, so wouldn't it be useful to have a way of telling them apart from the main feed for example?
What about feed versions of the is_category() type conditionals eg. something like is_category_feed(), is_tax_feed() etc...?
My use case for this was generating alternate views of the same context for a WordPress to kindle feed.

It doesn't make sense to return true for is_home or is_singular in a feed. It's a feed so is_feed returns true. You need to define your own query for the feed content.
Example:
add_feed( 'my-feed', 'my_feed_template' ); function feed_template() { load_template( ABSPATH . WPINC . '/feed-rss2.php' ); } add_action( 'pre_get_posts', 'my_feed_content' ); function feed_content( $query ) { // Bail if $posts_query is not an object or of incorrect class if ( ! is_object( $query ) || ( 'WP_Query' != get_class( $query ) ) ) return; // Bail if filters are suppressed on this query if ( $query->get( 'suppress_filters' ) ) return; if ( ! $query->is_feed( 'my-feed' ) ) return; $query->set( 'post_type', array( 'post', 'page' ) ); }