Yesterday, I installed wordpress on the server where my website hosted, and I decided to show several of my latest posts on my home page in order to let my visitors know my roadmap in real-time. Also – since I am using smarty templates to to facilitate the separation of application code from presentation, I don’t really want output (echo) the post data directly onto the page. After playing with the loop process for a while, I finally got this sorted out. Here is how I did it:
<Step One> Include the wp-blog-header.php file.
require('blog/wp-blog-header.php');
<Step Two> Collect all post data as required.
query_posts('showposts=4'); //Show latest 4 posts
while (have_posts()) //checks if any posts collected
{
static $blog = 0;
the_post(); //prepare for the iteration of the loop
$title[$blog] = the_title('','',0); //the_title function has 3 parameters, including 'start', 'end' and 'output or not'
$url[$blog] = get_permalink(); //post link
$date[$blog] = the_date("M d, Y", '', '', 0); //the_date function has 4 parameters, including 'date format', 'start', 'end', and 'output or not'
$blog++;
}
$numberOfPosts = $blog; //$numberOfPosts will be assigned to the loop number in the smarty template file.
Note – I created 3 arrays to store the relevant post information from the blog, including $title, $url, and $date, and then I use smarty template to display all them onto my home page (Demo Page)
Have Fun!
The post is really good. It is very well written. I simply like the style of writing.