Reputation: 11
I am hosted by hostinger or I have a wordpress site with its own database. I also have another site made in html php in a subdomain. I would like to be able to display a wordpress rss feed with a thumbnail on my site in php / html.
So I have a page that I host on my site in php / html which allows me to collect the title, a link and the description, but I would have liked to add the thumbnail.
<?php
if($flux = simplexml_load_file('http://clone.pecheperle.be/category/material/feed/'))
{
$donnee = $flux->channel;
// Initialiser le compteur.
$compteur = 0;
// Affiche les 5 derniers articles.
$limite = 5;
foreach($donnee->item as $valeur)
{
echo '<p>'.date("d/m/Y",strtotime($valeur->pubDate)).'';
echo ' - <a target="_blank" href="'.$valeur->link.'">'.$valeur->title.'</a>';
echo ' '.$valeur->description.'</p>';
// Le compteur incrémente +1 à chaque affichage.
$compteur++;
// Arrête d'afficher les articles quand la valeur limite est atteinte.
if ($compteur == $limite)
break;
}
}
else {
echo 'Le flux RSS du site n\'a pas pu être chargé.';
}
?>
Upvotes: 0
Views: 850
Reputation: 26
Thumbnails are not part of the default feed/RSS definition; But you can extend its format with the media specification ( see https://www.rssboard.org/media-rss ).
So you have to add one or more media:content tags into the items of the feed. In WP you usually have more as one image size (medium, large, post-thumbnail, ...), so you add all you need. So it would look like this:
<media:content url="http://DOMAIN/wp-content/uploads/sites/9/2021/11/sampleimage-300x195.jpg"
medium="image" width="300" height="195" type="image/jpeg" >
<media:description type="plain"><![CDATA[medium]]></media:description>
</media:content>
<media:content url="http://DOMAIN/wp-content/uploads/sites/9/2021/11/sampleimage-1024x666.jpg"
medium="image" width="1024" height="666" type="image/jpeg" >
<media:description type="plain"><![CDATA[large]]></media:description>
</media:content>
<media:content url="http://DOMAIN/wp-content/uploads/sites/9/2021/11/sampleimage-231x150.jpg"
medium="image" width="231" height="150" type="image/jpeg" >
<media:description type="plain"><![CDATA[post-thumbnail]]></media:description>
</media:content>
To add this in wordpress you have to create a plugin for this or add it in your theme.
<?php
public function add_media_thumbnail() {
global $post;
if( has_post_thumbnail( $post->ID )) {
$thumb_ID = get_post_thumbnail_id( $post->ID );
$data = wp_get_attachment_metadata( $thumb_ID );
$details = wp_get_attachment_image_src($thumb_ID, 'full');
$size = @filesize( get_attached_file( $thumb_ID ) );
echo '<enclosure url="' . $details[0] . '" length="' . $size . '" type="image/jpg" />' . "\n";
foreach ( $data['sizes'] as $_size => $sizedata) {
$details = wp_get_attachment_image_src($thumb_ID, $_size);
$out = '';
if( is_array($details) ) {
if ( in_array( $_size, array('thumbnail') ) ) {
$out .= '<media:thumbnail url="'.$details[0]. '" width="' . $details[1] . '" height="' . $details[2] . '" />' . "\n";
} else {
if ( in_array( $_size, $options['filter_sizes'] ) ) {
$out .= '<media:content url="'.$details[0].'" medium="image" width="'.$details[1].'" height="'.$details[2].'" type="'.$sizedata['mime-type'].'" >';
$out .= '<media:description type="plain"><![CDATA['.$_size.']]></media:description>';
$out .= ' </media:content>';
$out .= "\n";
}
}
echo $out;
}
}
}
}
// add the namespace to the RSS opening element
public function add_media_namespace() {
echo "xmlns:media=\"".$options['add_namespace']."\"";
}
?>
Then enhance the default actions for rss with the functions:
public function do_thumbnails2rss() {
add_action( 'rss2_ns', 'add_media_namespace' );
add_action( 'rss2_item', 'add_media_thumbnail' );
}
After this you can parse the feed and get the thumbnail in the size you need.
Upvotes: 1