Support » Developing with WordPress » Custom RSS with the_excerpt

  • I have a custom RSS feed with the following code snippet:

    <description><?php echo '<![CDATA['.'<p><span>by </span><strong>'.get_post_meta($post->ID,'author',true).'</strong></p><br>'.the_excerpt_rss($post->ID).'<a href="'.get_permalink($post->ID).'">Read the rest of this story.</a><br><br><br>'.']]>'; ?></description>

    Is that the proper use of the_excerpt? This doesn’t work for some reason.

    Here is the complete file:

    <?php
    /*
    Template Name: Custom Feed
    */
    
    $numposts = 7;
    
    function ffm_rss_date( $timestamp = null ) {
      $timestamp = ($timestamp==null) ? time() : $timestamp;
      echo date(DATE_RSS, $timestamp);
    }
    $posts = query_posts('showposts='.$numposts);
    
    $lastpost = $numposts - 1;
    
    header("Content-Type: application/rss+xml; charset=UTF-8");
    echo '<?xml version="1.0"?>';
    ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
      <title>Website</title>
      <link>https://website.org/</link>
      <atom:link href="https://website.org/feed/ac" rel="self" type="application/rss+xml" />
      <description>The latest stories from Website</description>
      <language>en-us</language>
      <lastBuildDate><?php ffm_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
      <managingEditor>my@email.org (My name)</managingEditor>
    
      <item>
        <title><?php echo get_the_title($post->ID); ?></title>
        <link><?php echo get_permalink($post->ID); ?></link>
        <pubDate><?php ffm_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
    <description><?php echo '<![CDATA['.'<p><span>by </span><strong>'.get_post_meta($post->ID,'author',true).'</strong></p><br>'.the_excerpt_rss($post->ID).'<a href="'.get_permalink($post->ID).'">Read the rest of this story.</a><br><br><br>'.']]>';  ?></description>
        <guid><?php echo get_permalink($post->ID); ?></guid>
      </item>
    <?php } ?>
    </channel>
    </rss>
Viewing 3 replies - 1 through 3 (of 3 total)
  • the_excerpt_rss() does not take any parameters and must be used within the loop. See the codex https://codex.wordpress.org/Template_Tags/the_excerpt_rss for more info on how to use.

    How should I change the code to display the excerpt then?

    Moderator bcworkz

    (@bcworkz)

    First of all, you should not use query_posts(), as explained in its docs page. You are much better off using the “pre_get_posts” action to setup the query as desired anytime is_feed() is true. The only query var you need to set is “posts_per_page”, which is the modern equivalent of “showposts”.

    The Loop is simply a while/endwhile loop that runs if and while have_posts() is true. The first call within the Loop is the_post(), which sets up some variables that template tags like the_excerpt_rss() use to know what the current post is. All of your post related output should be within the Loop.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom RSS with the_excerpt’ is closed to new replies.