Latest blog posts wordpress on another site in PHP

1

I need to put in a landingpage that is in HTML the last posts of a blog that is in Wordpress, I got a code on the internet that after the posts but I can not define how many posts will appear and nor get the image of the post. Can someone help me?

If you have any other code that does this too.

Thank you.

<?php
            $url = 'http://blog.empresa.com.br/category/empresa/feed/';
            $xml = simplexml_load_file($url);
            $item = '1';


            if($xml !== false){
            echo '<div class="blog">';
            foreach($xml->channel->item as $node){
            printf('<h5><a href="%s">%s</a></h5>', $node->link, $node->title);
            printf($node->description);
            }
            echo '</div>';
}?>
    
asked by anonymous 09.03.2018 / 19:33

1 answer

2

As I mentioned, there are several ways to resolve this using PHP

Or even using components ready for wordpres or other CMS's

Since it was not specified if the target site was built using some CMS, it follows an example using DOMDocument in PHP

<?php
    $rss = new DOMDocument();
    $rss->load('http://blog.empresa.com.br/category/empresa/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,
            );
        array_push($feed, $item);
    }
    $limit = 5; //Quantidade que deseja exibir
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        echo '<small><em>Postado em '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
    }
?>
    
09.03.2018 / 19:45