In WordPress blog, our latest posts are publicized on the home page, and older posts access with the help of post page navigation. In this post, we are going to learn how to display our WordPress blog posts on any static or dynamic page using a simplexml_load_file() method with ATOM that works to get WordPress blog post details as XML data with domain link.
There is one pretty Impressive fact that every WordPress blog has “Atom” specified XML file it will only to serve the Data into other servers. Only we need to call it using a PHP method named simplexml_load_file() which simply stores the XML file and changes the file toward Pure XML Detail and then all the data can be accessed in object data format.
Step 1: Making connection with WordPress blog
Load the data into XML data object using simplexml_load_file() and Atom method. It will request to given URL link and get the dataset from it.
1 |
$post_data=simplexml_load_file("url_path_to_wordpress_blog/feed/atom") or die("Error Information: Can't make connection object"); |
Step 2: Display the blog post details
Here we need to get the required details from XML object like Title, Published By, Published Date, Post Description and etc.
1 2 3 4 5 6 |
foreach($post_data->entry as $post_details) { $post_date=date('F d, Y', strtotime($post_details->published)); $post_title=$post_details->title; $post_author_name=$post_details->author->name; $post_description=$post_details->summary; } |
Final Code
Here the full final code to get WordPress blog post and show on our page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $post_data=simplexml_load_file("url_path_to_wordpress_blog/feed/atom") or die("Error Information: Can't make connection object"); foreach($post_data->entry as $post_details) { $post_date=date('F d, Y', strtotime($post_details->published)); $post_title=$post_details->title; $post_author_name=$post_details->author->name; $post_description=$post_details->summary; ?> <tr> <th width="5%">Title</th><td width="40%"><?php echo $post_title;?></td> <th width="10%">Published By</th><td width="20%"><?php echo $post_author_name;?></td> <th width="5%">Date</th><td width="10%"><?php echo $post_date;?></td> </tr> <tr> <td width="100%" colspan="6" class="td-description"><b>Description:</b><br/><?php echo $post_description;?></td> </tr> <tr><td colspan="6"> <br/> </td></tr> <?php } ?> |