Latest posts wordpress blog on another site in PHP

Asked

Viewed 339 times

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 brings the posts more I can not define how many posts will appear and nor take the image of the post. Someone can help me?

If you have any other code that does that 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>';
}?>
  • in this example you are loading the feed generated by worpress, one option is to configure it in wordpress. Another option is to manipulate this xml better to control the quantity in this loop.

1 answer

2


As I mentioned, there are many ways to solve this using PHP

Or even using components ready for Wordpres or other CMS’s

As it was not specified if the target site was built using some CMS, follow 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>';
    }
?>
  • Landingpage is not using any CMS, only HTML and CSS, I put PHP that I posted just to pull the last blog posts that is in wordpress

  • if you want to display without PHP, you can do this using only JS tbm https://gist.github.com/juanbrujo/3a71489572e5677fcec7e14676aa66e2

  • conseguei com esse código php que vc postou acima, só fez Alguamas alterações para limito número de caracteres da descrição, for example. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.