Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

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:
» Introduction To WordPress
» WordPress Themes
» Structure Of A WordPress Theme
» Making Your New Theme
» Modifying The Header
» Blog / Post Templates
» The Post Loop
» Page Templates
» Sidebars
» Meta Data / Custom Fields For Page Customization


Originally posted on Makebeta


 | Bookmark
Related Tutorials:
» Setting Up Subversion for Development on Windows
» Stylize Your Digg Count
» Installing Apache on Windows
» Ecommerce Imagery: Persuading with Pictures
» Customizable Websites - The Definitive Guide
» 7 Usability Guidelines for Websites on Mobile Devices

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources