<?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>Leo Newball, Jr. &#187; Recent Posts</title>
	<atom:link href="http://leonewball.com/tag/recent-posts/feed/" rel="self" type="application/rss+xml" />
	<link>http://leonewball.com</link>
	<description>life, design, tech</description>
	<lastBuildDate>Sat, 03 Apr 2010 05:53:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Managing Recent Posts in Word Press</title>
		<link>http://leonewball.com/2008/08/05/managing-recent-posts-in-word-press/</link>
		<comments>http://leonewball.com/2008/08/05/managing-recent-posts-in-word-press/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 16:00:53 +0000</pubDate>
		<dc:creator>Leo Newball, Jr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Recent Posts]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP_Query()]]></category>

		<guid isPermaLink="false">http://leonewballjr.name/design/managing-recent-posts-in-word-press/</guid>
		<description><![CDATA[One problem I came across in making the theme “So Fresh So Clean” (to be discussed in a future blog post) was attempting to display a list of my recent blog posts without needing to use a sidebar widget. The theme is influenced by many different web designs, but borrows a few concepts from Dave [...]]]></description>
			<content:encoded><![CDATA[<p>One problem I came across in making the theme “So Fresh So Clean” (to be discussed in a future blog post) was attempting to display a list of my recent blog posts without needing to use a sidebar widget. The theme is influenced by many different web designs, but borrows a few concepts from Dave Shea’s website <a href="http://mezzoblue.com/">Mezzoblue</a>. The idea came from the book <a href="http://www.amazon.com/gp/product/0321526287?ie=UTF8&amp;tag=leonewballjr-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321526287">Building Findable Websites: Web Standards SEO and Beyond</a><img style="margin: 0px;border-top-style: none! important;border-right-style: none! important;border-left-style: none! important;border-bottom-style: none! important" src="http://www.assoc-amazon.com/e/ir?t=leonewballjr-20&amp;l=as2&amp;o=1&amp;a=0321526287" border="0" alt="" width="1" height="1" />. In the section <em>Displaying Your Most Recent Posts</em>, Aaron Walter discusses Dave’s technique for displaying one post on his front page, and a list of the most recent posts in the top section of his website. Dave suggests that in order to do this you can use the following code in the <a title="The WordPress Loop" href="http://codex.wordpress.org/The_Loop">WordPress loop</a>:</p>
<p><code>&lt;ul id="recent-posts"&gt;<br />
&lt;?php $posts = get_posts('numberposts=5&amp;offset=1');<br />
foreach ($posts as $post): ?&gt;<br />
&lt;li&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;<br />
&lt;?php endforeach; ?&gt;<br />
&lt;/ul&gt;</code></p>
<p>While this code does work, it can be problematic, the get_post() query overwrites the WordPress default variables. This means if you run any other loop specific code (i.e. the_title, the_content, the_date, etc.) they will all be affected and many not produce desired results. In my initial testing every page that used The Loop was getting it’s information from the get_posts() query as it overwrites WordPress’ default queries for pages, searches and archives. Thus they all displayed the same information.</p>
<p>After some searching I found a solution for the problem, creating a new <a title="WP_Query() Function Reference" href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query()</a> object so that it doesn’t interfere with WordPress default functions. Here’s an updated example of the code presented above using this technique.</p>
<p><code>&lt;ul id="recent-posts"&gt;<br />
&lt;?php $recent_posts = new WP_Query(); $recent_posts-&gt;Query('showposts=5@offset=1');<br />
while ($recent_posts-&gt;have_posts()) : $recent_posts-&gt;the_post(); ?&gt;<br />
&lt;li&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;<br />
&lt;?php endwhile; ?&gt;<br />
&lt;/ul&gt;</code><br />
If you want something a bit fancier, here’s how I created the recent posts section using the same technique.</p>
<p><code>&lt;div id="recent_posts"&gt;<br />
&lt;h2&gt;Recent Posts&lt;/h2&gt;<br />
&lt;?php $recent_posts = new WP_Query(); $recent_posts-&gt;Query('showposts=5@offset=1');<br />
while ($recent_posts-&gt;have_posts()) : $recent_posts-&gt;the_post(); ?&gt;<br />
&lt;div class="recent_date"&gt;&lt;?php the_time('m.d.Y') ?&gt;&lt;/div&gt;<br />
&lt;div class="recent_post_title"&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/div&gt;<br />
&lt;br /&gt;<br />
&lt;?php endwhile; ?&gt;<br />
&lt;/div&gt;</code><br />
A deeper discussion in how to do this is on the <a href="http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/">Weblogs Tools Collection</a> (where I found the solution). The lesson here is, in order to avoid conflicts with WordPress default functions, plugin, and countless other issues, create a new WP_Query(); object.</p>
]]></content:encoded>
			<wfw:commentRss>http://leonewball.com/2008/08/05/managing-recent-posts-in-word-press/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
