24 Avr Récupérer le flux RSS dans un autre site
Si vous avez besoin de récupérer le flux RSS d’un site WordPress, par exemple d’une catégorie spécifique, c’est très simple il faut juste avoir la bonne URL.
<?php
$rss = new DOMDocument();
$rss->load('http://www.DOMAINE/CATEGORIE/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
/* ICI ON INSERE TOUS LES ARTICLES DANS UN TABLEAU */
array_push($feed, $item);
}
/* ICI ON RECUPERE LES 4 DERNIERS ARTICLES */
for($j=0;$j<'4';$j++){
/* RECUPERATION DU TITRE */
echo utf8_decode($feed[$j]['title']).'<br />';
/* RECUPERATION DE LA DATE AVEC LA MISE EN FORME DE LA DATE */
echo date("d M Y H:i:s", strtotime($feed[$j]['date'])).'<br />';
/* RECUPERATION DE L IMAGE */
$doc=new DOMDocument();
$doc->loadHTML(utf8_decode($feed[$j]['desc']);
$xml=simplexml_import_dom($doc);
$images=$xml->xpath('//img');
foreach ($images as $img) {
echo "<img src='".utf8_decode($img['src'])."' alt='' /><br />";
}
/* RECUPERATION DU DESCRIPTION */
$content = $feed[$j]['desc'];
$content = preg_replace("/<img[^>]+\>/i", "", $content);
echo $content.'<br />';
}
Pour la partie image n’existe pas dans le FEED du WordPress, donc on doit l’ajouter avec l’ajout d’une fonction dans notre fichier functions.php
function featuredtoRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');