Helping ordinary people create extraordinary websites!

WordPress Customization

By Justin Laing
2008-01-09

The Post Loop

So you want a custom loop? The loop is powered off of the current page query. The query is generated by WordPress, typically from your URL. So if your on your site and your at /category1/a-blog-post-with-id-12 the query to get blog post id 12 in category 1. The internal mechanics of how this happens are fairly complex, so we won’t go into them. It’s enough to know that each page has a query generated for it that will grab the correct content for that page. So if you want to customize your loop and grab something besides what WordPress would do by default you need to modify the query before you call your loop.

Let’s say you want to put just the 3 most recent blog posts on a page, maybe under some other content. On my site the home page has static content and then displays some recent blog posts. Here’s how you do it:

<?php query_posts(”showposts=3″);
?>

This does a new query to grab the 3 most recent posts to the blog. I can then display these posts in a normal loop:

<?php if (have_posts()) {
?>


<h3>Recent
News</h3>


<?php while (have_posts()) :
the_post(); ?>

<div
class=”blog_post”>



<?php the_content();
?>



</div>


<?php endwhile;
?>


<?php } ?>


Loop Example 3 Posts

There are almost infinite ways you can write queries to get the posts you want for your loop. Visit these two pages for more information:

http://codex.wordpress.org/The_Loop
http://codex.wordpress.org/Function_Reference/WP_Query





Tutorial pages:

Originally posted on Makebeta


 3 Votes

You might also want to check these out:


Leave a Comment on "WordPress Customization"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS