0

I am trying to create a custom XML RSS feed. However, my code is not quite working as expected. Here is what I have done:

In functions.php I have created the feed called "top":

add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'top');
        get_template_part('rss', 'top');
}

In my child theme I have created file rss-top.php which contains my template file.

I am looking for a quite simple template structure like below:

<?xml version="1.0" encoding="UTF-8"?>
<articles>
    <article>
        <title>Overskrift</title>
        <teaser>Short excerpt</teaser>d
        <link>http://link-to-post.com/</link>
        <image>http://placehold.it/100x100</image>
        <date>19-06-2017 11:21:00</date>
    </article>
    <article>
        <title>Overskrift</title>
        <teaser>Short excerpt</teaser>
        <link>http://link-to-post.com/</link>
        <image>http://placehold.it/100x100</image>
        <date>19-06-2017 11:21:00</date>
    </article>
</articles>

I have tried creating the structure like so:

<?php
/**
 * Template Name: Custom RSS Template - top
 */
$postCount = 6; // The number of posts to show in the feed
$posts = query_posts('showposts=' . $postCount);
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>

<articles>
        <?php while(have_posts()) : the_post(); ?>
                <article>
                        <title><?php the_title_rss(); ?></title>
                        <teaser><![CDATA[<?php the_excerpt_rss() ?>]]></teaser>
                        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
                        <link><?php the_permalink_rss(); ?></link>
                        <image><?php get_the_post_thumbnail( $post->ID ); ?></image>
                        <date><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></date>
                        <?php rss_enclosure(); ?>
                        <?php do_action('rss2_item'); ?>
                </article>
        <?php endwhile; ?>
</articles>

Afterwards I have updated the permalink and then trying to view site.com/rss/top.xml

However, for some reason, it just shows the latest couple of posts like it was an archive page on my site and not as an XML file as expected. Any pointers on what I might be missing ??

2
  • If the goal of this is to provide the latest posts but with a custom number of posts in the feed, you don't need to do this. A pre_get_posts filter will do this for you, or even adding ?posts_per_page=6 to the end of the main RSS feed Commented Sep 2, 2021 at 14:24
  • adding ?posts_per_page=6 to main RSS feed won't do the job since the main feed is not following the structure I am aiming for :( Commented Sep 2, 2021 at 14:28

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.