Skip Navigation

[Résolu] Add Custom Field image to RSS

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 réponses, has 3 voix.

Last updated by Ronald E Il y a 8 années.

Assigned support staff: emerson.

Auteur
Publications
#135743

Hi guys and ladies,

I am trying to add my images which are set in Custom Fields to the RSS feeds. I am trying to use the code below but nothing seems to be happening. What am I missing please?

/*Customise RSS Feeds*/
function traveltips_postrss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$traveltipimage = get_post_meta($postid, 'wpcf-featured-travel-tip-image', true);
if(is_feed()) {
if($traveltipimage !== '') {
$content = $content."<br /><br /><div>".$traveltipimage."</div>
";
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'traveltips_postrss');
add_filter('the_content', 'traveltips_postrss');
#136017

Dear Marcel,
Usually to add content to the feeds, there are two filters used. You have the "the_excerpt_rss" but you don't have the "the_content_feed". Can you add this filter too? So your final set of filters would be:
[php]
add_filter('the_excerpt_rss', 'traveltips_postrss');
add_filter('the_content_feed', 'traveltips_postrss');
[/php]

Of course, I assume here that your function code correctly fetches the custom field image and that $traveltipimage has an output. Please let me know how it goes. Thanks.

Cheers,
Emerson

#136066

Yup, got the image. 🙂 One final thing. I am trying to get the image to resize in the browser using width / height attributes

$content = $content."<br /><br /><img src=".$traveltipimage." width="144px" height="80px"><br />

but I keep getting a server error as soon as I put them in. Any idea?

#136376

Dear Marcel,
Yes I see some syntax error above, instead of using double quotes for image size in pixels use single quote within double quotes, example:

[php]
$content = $content.&quot;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;.$traveltipimage.&quot; width='144px' height='80px'&gt;&lt;br /&gt;&quot;;
[/php]

The final code is this:
[php]
function traveltips_postrss($content) {
global $wp_query;
$postid = $wp_query-&gt;post-&gt;ID;
$traveltipimage = get_post_meta($postid, 'wpcf-featured-travel-tip-image', true);
if(is_feed()) {
if($traveltipimage !== '') {
$content = $content.&quot;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;.$traveltipimage.&quot; width='144px' height='80px'&gt;&lt;br /&gt;&quot;;
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'traveltips_postrss');
add_filter('the_content_feed', 'traveltips_postrss');
[/php]

Cheers,
Emerson

#136382

Excellent thanks!

#151625

I'm looking to do the same for my RSS feed.

Does the above code go into the Theme's Functions.php? Or would it go into feeds.php in the wp-includes directory? or somewhere else?

#151637

in addition - "and that $traveltipimage has an output."

Does this mean adding a custom field called traveltipimage to the RSS2 template?