Wordpress latest articles on homepage or different website
I was looking for a solution to this and found that it’s pretty easy to achieve this. Here are the simple steps:
Step 1: Add this code at the start of your page. This has to come even before the <Html> tags on top.
<?php
define(’WP_USE_THEMES’, false);
require(’india-news/wp-blog-header.php’);
query_posts(’showposts=1′);
?>
Step 2: Now on the page, probably your homepage, where you want the title and latest post to display, add this:
<?php while (have_posts()): the_post(); ?>
<?php endwhile; ?>
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p><a href=”<?php the_permalink(); ?>”>Read more…</a></p>
<?php endwhile; ?>
That is it. You are good to go. If you need any more options to be displayed, you can go to your wordpress template folder and from the index.php file, copy the code which is inside the “the_content()” tags.
If you want the wordpress latest post to display on another website, all you need to do is update your Step 1 code:
<?php
define(’WP_USE_THEMES’, false);
require(’/var/www/example.com/blog/wp-blog-header.php’);
query_posts(’showposts=1′);
?>
Another Method: Using RSS Feeds
Add this code to where you want the feed to display: <?php $feed_url = ‘http://yoursitename/?feed=rss/’; $max_items = 5; //Load SimplePie include ‘inc/simplepie.inc’; //Fetch the RSS feed $feed = new SimplePie($feed_url); //Check for errors if ($feed->error()) echo ‘Error : ‘,$feed->error(); //Output up to $max_items posts foreach ($feed->get_items(0, $max_items) as $item): ?> <div class=”item”> <h3 class=”title”><a href=”<?php echo $item->get_permalink(); ?>“><?php echo $item->get_title(); ?></a></h3> <?php echo $item->get_description(); ?> <p><small> Posted <?php if ($author = $item->get_author()){ echo ‘ by ‘.$author->get_name(); }?> on <?php echo $item->get_date(‘j F Y | g:i a’); ?> </small></p> </div> <?php endforeach; ?> Download Simplepie and just add the file simplepie.inc from the downloaded file to your ftp location mentioned above For eg: ( /inc/simplepie.inc). If you want to display wordpress posts from a single category, then just change the feed url above to point to your category, Eg:$feed_url = ‘http://yoursitename/?cat=26&feed=rss2′;
Need your advise. I need to post only 3 latest articles from category. How can I do that. Thanks.
Comment by Yegor — October 1, 2009 @ 6:57 pm
In the code, you will see a line “$max_items = 5;”
Just set that to 3, and only 3 items will be displayed.
Comment by admin — October 21, 2009 @ 2:38 am