<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The WordPress Guru &#187; The Loop</title>
	<atom:link href="http://wpguru.co.za/category/the-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpguru.co.za</link>
	<description>Tips &#38; Pointers on Making a Killer CMS with WordPress</description>
	<lastBuildDate>Mon, 01 Aug 2011 10:26:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Own your WordPress loop with wp_query</title>
		<link>http://wpguru.co.za/advanced/the-loop/own-your-wordpress-loop-with-wp_query/</link>
		<comments>http://wpguru.co.za/advanced/the-loop/own-your-wordpress-loop-with-wp_query/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 09:42:37 +0000</pubDate>
		<dc:creator>MarkB</dc:creator>
				<category><![CDATA[The Loop]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[wp query]]></category>

		<guid isPermaLink="false">http://wpguru.co.za/?p=38</guid>
		<description><![CDATA[The WordPress loop is arguably the heart of any WP powered site. With all this power at its fingertips, it obviously has some serious customization potential. I'm going to explain a couple of ways here of how you can fully use the potential of your WP CMS's loop to display custom content anywhere on your site. Get ready to own your WP Loop!]]></description>
			<content:encoded><![CDATA[<div id="attachment_139" class="wp-caption alignright" style="width: 310px"><img class="size-medium wp-image-139 " title="WordPress Loop" src="http://wpguru.co.za/wp-content/uploads/loop-300x200.jpg" alt="" width="300" height="200" /><p class="wp-caption-text">Image by yaz36</p></div>
<p>The WordPress loop is arguably the heart of any WP powered site. With all this power at its fingertips, it obviously has some serious customization potential. I&#8217;m going to explain a couple of ways here of how you can fully use the potential of your WP CMS&#8217;s loop to display custom content anywhere on your site. Get ready to <a href="http://www.urbandictionary.com/define.php?term=own" target="_blank">own</a> your WP Loop!</p>
<h3>The Scenario</h3>
<p>Remembering that content is king, I always recommend to my clients that they have a blog of some sort: a style blog, <a href="http://www.pokerblog.com/">Pokerblog</a>, Tech blog &#8211; whatever it is they do, they need a blog. Thus somewhere in your custom WP theme, you&#8217;re going to want to display maybe the latest 2 or 3 articles from their blog. Usually, you could just use <code>query_posts()</code> to bring in posts from a specific category.</p>
<p>However, I&#8217;ve found in the past that if I use this method before the normal loop for the page&#8217;s content, it messes with the functionality of the main loop because of issues with <code>rewind_posts()</code> and such.</p>
<p>To altogether avoid this issue, it&#8217;ll be best if you get used to making your own WP query.</p>
<h3>Making your loop</h3>
<p>Before looping, you need to tell WordPress what to loop through. So firstly, setup your query.</p>
<pre class="brush: php; title: ; notranslate">&lt;?
 $recentPosts = new WP_Query();
 $recentPosts-&gt;query('showposts=5');
?&gt;</pre>
<p>Then you can join it up with a loop to process the information that your query finds.</p>
<pre class="brush: php; title: ; notranslate">&lt;? while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;
&lt;!-- LOOP STUFF HERE --&gt;
&lt;?php endwhile; ?&gt;</pre>
<p>So when you add it all together:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php
 $recentPosts = new WP_Query();
 $recentPosts-&gt;query('showposts=5');
 while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post();
?&gt;
&lt;!-- LOOP STUFF HERE --&gt;
&lt;?php endwhile; ?&gt;</pre>
<p>The query itself can have <a href="http://codex.wordpress.org/Template_Tags/query_posts" target="_blank">many different arguments</a> to help you filter down to exactly what you&#8217;re looking for. For example:</p>
<ul>
<li><code>cat=3</code> &#8211; specify the category that you want to display. If you have more posts that you want to include than exclude, then it might be easier to just filter out the ones you don&#8217;t want to see by using: <code>cat=-1, -2, -3</code> etc</li>
<li><code>showposts=3</code> &#8211; specify how many posts you want to show</li>
<li><code>orderby=name</code> &#8211; determine how you want your results to be ordered by: author, date, category, id, title, modified, parent and rand. orderby=rand is a great means of having a news area that displays a random news article from the archives.</li>
<li><code>year=2004</code> &#8211; display posts from only a specific year</li>
</ul>
<p>You can string a whole bunch of these arguments together like so:</p>
<pre class="brush: php; title: ; notranslate">&lt;? query_posts('cat=3,4,5&amp;showposts=10&amp;orderby=date'); ?&gt;</pre>
<p>With a little imagination, your query can retrieve any number of configurable results.</p>
]]></content:encoded>
			<wfw:commentRss>http://wpguru.co.za/advanced/the-loop/own-your-wordpress-loop-with-wp_query/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

