0

I've been using an added function to load a modified rss template. Works fine, but now I need to add a custom template for a CPT.

I have code for each,which works ok, but can't be used together because one over-rides the other.

I don't know enough php to modify.

Here's the code I'm using...

remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', function() {
if ( $rss_template = locate_template( '/feeds/notes-feed.php' ) )
load_template( $rss_template );
else
do_feed_rss2(); // Call default function
}, 10, 1 );

and

remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'item_feed_rss2', 10, 1 );
function item_feed_rss2() {
$rss_template = get_template_directory() . '/feeds/item-feed.php';
if( get_query_var( 'post_type' ) == 'item' and file_exists(      $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2(); // Call default function

Not knowing enough, I'm wondering if I can use this...

remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'item_feed_rss2', 10, 1 );
function item_feed_rss2() {
$rss_template = get_template_directory() . '/feeds/item-feed.php';
$rss_template_notes = get_template_directory() . '/feeds/notes-feed.php';
if( get_query_var( 'post_type' ) == 'item' and file_exists( $rss_template ) )
load_template( $rss_template );
elseif( get_query_var( 'post' ) and file_exists( $rss_template_notes ) )
load_template( $rss_template_notes );
else
do_feed_rss2(); // Call default function  
1

Answering my own question, I’ll add this in case it’s of use to someone.

It seems to work ok with…

// This delivers valid feeds, with the correct templates.
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', function() {
$rss_template = get_template_directory() . '/feeds/item-feed.php';
$rss_template2 = get_template_directory() . '/feeds/notes-feed.php';
//if ( $post_type = 'item'  )
if( get_query_var( 'post_type' ) == 'item' and file_exists($rss_template ) )
load_template($rss_template);
elseif ( $post_type = 'post' )
load_template($rss_template2);
else
do_feed_rss2(); // Call default function
}, 10, 1 );

This enables use of a custom template for the feed of normal posts, and use of a different custom template for the feed of the CPT ‘item’.

The feeds differ in channel title/link/description.

| improve this answer | |
  • Nothing else worked. – wp student May 24 '18 at 4:43
0

Just combine the logic for the two together. :)

Use a single callback and depending on current context override with either one template or another.

Another option would be to handle differences inside the template, not my favorite approach but some prefer it.

| improve this answer | |
  • Thanks. My php knowledge is small, so I've tried to use your suggestion and add to the original question. – gulliver Sep 3 '16 at 7:33

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.