Expanding on the WordPress Mini-Loop
Those of you who’ve found yourself in need of a static front page for your WordPress powered site have no doubt thought about the addition of the “Mini-Loop” to bring a few blog posts to your front page. I think it’s also safe to say that the majority of you have been disappointed with the example from the documentation:
[code lang="php"]
$how_many=5; //How many posts do you want to show
require_once(”wp-config.php”); // Change this for your path to wp-config.php file ?>
-
echo get_permalink($np->ID);
print (”\”>$np->post_title
$news=$wpdb->get_results(”SELECT `ID`,`post_title` FROM $wpdb->posts
WHERE `post_status`= \”publish\” ORDER BY ‘ID’ DESC LIMIT “.$how_many);
foreach($news as $np){
print (”
“);
}
[/code]
All that and what do you get? Just the headline in an ugly ordered list. No lead-in text, no date, nothing. Now, considering content is king in the world of Search Engine Optimization, I wanted to bring a little more to the plate then just a simple list of headlines. The process is actually very, very simple once you do a little clean up job. Lets take a look at my first pass:
[code lang="php"]
$how_many=5;
require_once(”wp-config.php”);
$news=$wpdb->get_results(”SELECT ID, post_title FROM $wpdb->posts
WHERE post_status=’publish’ ORDER BY ID DESC LIMIT $how_many”);
foreach($news as $np){
echo ‘
$how_many=1;
require_once(”wp-config.php”);
$news=$wpdb->get_results(”SELECT ID, post_title, post_excerpt FROM $wpdb->posts
WHERE post_status=’publish’ ORDER BY ID DESC LIMIT $how_many”);
foreach($news as $np){
echo ‘
$how_many=1;
require_once(”wp-config.php”);
$news=$wpdb->get_results(”SELECT ID, post_title, post_excerpt, post_date FROM $wpdb->posts
WHERE post_status=’publish’ ORDER BY ID DESC LIMIT $how_many”);
foreach($news as $np){
echo ‘
strtotime and date functions. With strtotime(), we can convert the date we’re getting from MySQL into a UNIX timestamp, then, with date(), we can take that timestamp and turn it into a sexy looking format of our choice. For the example above, I choice something like “Monday, July 4th, 2006″ and cut out the time, but you can change that to anything you want. Just check out the PHP manual for a nice table of options.
require_once(”wp-config.php”);
$news=$wpdb->get_results(”SELECT ID, post_title, post_excerpt, post_date FROM $wpdb->posts
WHERE post_status=’publish’ ORDER BY ID DESC LIMIT $how_many”);
foreach($news as $np){
echo ‘
strtotime and date functions. With strtotime(), we can convert the date we’re getting from MySQL into a UNIX timestamp, then, with date(), we can take that timestamp and turn it into a sexy looking format of our choice. For the example above, I choice something like “Monday, July 4th, 2006″ and cut out the time, but you can change that to anything you want. Just check out the PHP manual for a nice table of options.
One note about the date format is that it can be done directly from MySQL. Since it’s been my experience that most people working with WordPress have a very limited understanding of PHP, let alone MySQL, I chose the path of least resistance. If enough people whine about it, I’ll go ahead a put up an example of how that’s done.
RSS 2.0


















Hi,
Just wanted to say; excellent post. Really helpful.
I was wondering if you would be able to throw any light on how I could ammend the above to only show posts from a certain category? I’ve trawled through the Wordpress support forums and found that quiote a few people are looking for this.
Many thanks again.
Scott.
Hello,
First of all, great post. I found your explanations to be thorough and clear. I wish all bloggers and coders worked like you.
I’ve developed some questions concerning your code. I’ve tried it out myself, and it does work for me, except for the “excerpt” function. For some reason, no excerpt shows. Not sure why.
Also, I’m only using this code so I can modify it. I don’t want to show the most recent posts, I wish to show posts with a specific tag. For instance, I have one “static” page called “Comics” that I want to have dynamic content on. This code is the only way I could find in doing this. However, since it is my comics page, I want to be able to make a post, label it “comic”, not have it posted on my home page, and only have it posted on this new comic page, since it would be filtered out by the code. If you need more explanation, just comment back and I will reply.
Thanks again for the clean and useful code.
Jason
I am terribly sorry. I realized only after I posted my comment that I did not include excerpts in any of my recent posts, so naturally they didn’t show up. Sorry about that.
Cool post! I have a related question-
Do you know of a way to pull an image for each post as well?
Thanks-
fueler
I guess this page is broken, because what you have said here does nothing, and looks broken.
Just when I thought I found what I need
1fThank’s for greate post.6u I compleatly agree with last post. jxp
паркетная доска 3r
Side effect of amoxicillin anxiety….
Feline dose amoxicillin. Side effects from amoxicillin 500mg. Side effects of amoxicillin. Mix amoxicillin with frontline on cat….
Hi,
My Name is, Mark
Nice site, verry informative
my site:
http://pDXggn.spaces.live.com/
Tramadol cod….
Tramadol hydrochloride. Tramadol hcl 50mg. Tramadol. Ultram tramadol. Side effects tramadol….
It is beautifully turned out .. I liked ..) would be continuously zahazhivat to you.