0

I use wp as a cms. I needed a custom-made template for my rss feeds, so that I could display custom fields of my posts in the feeds. I took the basic custom rss template suggested by Yoast and made some modification as I needed. It is working fine except that I am unable to retrieve the featured-post-thumbnail and other custom-field-values.

Here is what my custom rss feed url looks like now - http://website.com/custom-feed/

Here is the complete code :

<?php
/*
Template Name: Custom Feed 
*/

$numposts = 5;
function yoast_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">
<channel>
<title>website content feeds</title>
<link>http://website.com/</link>
<description>The latest content from website.</description>
<language>en-us</language>
<pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
<lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
<managingEditor>director@website.com</managingEditor>
<?php foreach ($posts as $post) { ?>

<item>
<title><?php echo get_the_title($post->ID); ?></title>
<link><?php echo get_permalink($post->ID); ?></link>

<?php echo add_my_custom_field_node();?>

<description><?php $object = new WPSEO_Frontend(); echo $object->metadesc( false ); ?> </description>
<pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
<guid><?php echo get_permalink($post->ID); ?></guid>
</item>

<?php } ?>
</channel>
</rss>

The code that I am trying to pull the thumbnail is suggested in a solution by @bainternet to a similar question and I have it between the <link> and <description> tags. But it is not doing anything.

I tried using <?php echo get_post_meta($post->ID, 'location', true);?> for custom-feld-values to appear but it does not work either. Is there specific tags I should be using for the featured-image or custom-fields to appear, if so what tags and how do I use them? I also tried solution suggested here. What am I missing. Please guide, thanks.

FYI - My post descriptions are auto generated by the yoast-wordpress-seo plugin so for descriptions I am using a custom code, which is working fine.


UPDATE : Reading the solution by @BenMiller to this question I understood that I have to use rss2_item action hook in order to add tags to my custom-rss-feed. Here is a the code in my functions.php now :

 add_action('rss2_item', 'add_my_custom_field_node');

function add_my_custom_field_node() {
    global $post;
    $metaValue = get_post_meta($post->ID, 'location', true);
    if(!empty($metaValue)):
        echo("<my-custom-field>{$metaValue}</my-custom-field>");
    endif;
}

SECOND UPDATE : The other strange thing that I noticed is that when I checked my custom-rss-feed-url at validator.org. It shows my custom-field-values and the featured-image url with a .jpg extension but for some reason throws an error. Undefined item element: my-custom-field. I use livebookmarks to check feed-url and using firefox browser. I am totally confused.

  • from your file, remove this line <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); }?> and add this one - if( has_post_thumbnail($post->ID) ){ $thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID)); echo("<image>{$thumbnail}</image>"); } – Shazzad Jan 20 '14 at 9:30
  • @Shazzad I did the suggested but still cant see any image in the feeds. FYI - I have not added anything to my funtions.php yet. – gurung Jan 20 '14 at 9:36
  • Can you give me a link to you feed page ? – Shazzad Jan 20 '14 at 9:53
  • Please add add_my_rss_node() action in functions.php – shahpranaf Jan 20 '14 at 10:04
  • Ok, the image is there. You probably won't see this with browser, but any RSS feed parser will definitely find it. Now, if you want to display the image within the description, then you need to revert to your old code, and put back the line <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); }?> just after the tag <description>, before <?php $object = new WPSEO_Frontend();. – Shazzad Jan 20 '14 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.