código

Leer un feed de WordPress incluyendo las etiquetas con php

En el siguiente ejemplo vamos a obtener el contenido del Feed de un sitio en WordPress con PHP + simplexml_load_string. De igual forma si no deseamos obtener las etiquetas por cada publicación podemos utilizar SimpleXmlElement

<?php

$feed_url = "https://doroteocatalan.com/feed";

/* si no deseamos obtener las categorias 

$content = file_get_contents($feed_url);
$xml = new SimpleXmlElement($content);
*/

$buscar = array("<![CDATA[", "]]>");
$contenido = str_replace($buscar, '', file_get_contents($feed_url));
$xml = simplexml_load_string($contenido,'SimpleXMLElement');

$items = count($xml->channel->item);
if ( $items >0 ) { ?>
  <ul> <?php
    $stop = 5; // mostramos solamente 5 publicaciones
    $cont = 0;
    foreach( $xml->channel->item as $item ) {      
      if($stop==$cont) break;
      $cont++;
      $pubDate = date("d.m.Y H:i", strtotime((string) $item->pubDate)); ?>
      <li>
        <a href="<?php echo $item->link; ?>" target="_blank" title="<?php echo $item->title; ?>"><?php echo $item->title; ?></a><br />
        <?php echo $item->description; ?><br />
        Categorias: <?php        
        $i=0;
        foreach( $item->category as $category ) {        
          echo $category[$i].' || ';
          $i++;
        } ?>
        <time class="entry-date" datetime="<?php echo $pubDate; ?>"><?php echo $pubDate; ?></time>
      </li><?php
    } ?>
  </ul><?php
} ?>

En el siguiente ejemplo no vamos a obtener las etiquetas y utilizaremos simplexml_load_file

<?php
    // Ruta del RSS 
    $rssUrl = "https://doroteocatalan.com/feed";
    $xml2 = simplexml_load_file($rssUrl); // Carga de fichero XML    
    // XML format into the array
    $entry = $xml2->channel->item;    
    // solamente vamos a mostrar 5 elementos para este ejemplo 
    for ($i = 0; $i < 5; $i++) {
        echo "<h3>".$entry[$i]->title."</h3>"; // Título
        echo "<p>".$entry[$i]->link."</p>"; // Enlace
        echo "<p>".$entry[$i]->description."</p>"; //Contenido
        echo "<p>".$entry[$i]->pubDate."</p>"; // Fecha Publicación
        echo "<p>".$entry[$i]->author."</p>"; // Autor
    }
    ?>

<?php

En ambos casos estamos limitando a mostrar las ultimas 3 publicaciones, pero puedes recorrer todo el Feed y mostrar las publicaciones que están configuradas en el sitio del cual estamos mostrando los datos.

Puedes ver el ejemplo en el siguiente enlace

Ver ejemplo