<p>Recently, I had to slice and implement <a href="http://www.thematrixfiles.net/">a blog for the CEO of my company</a>. I decided to go with WordPress because that is what I have become enamored with recently, when <a href="http://www.wirelesscareer.com/">I created a job board</a> using it. I used some features of WordPress that I had never used before, so I figured I would talk about them so that others could learn how to use them as well.</p>

<h2>Multiple Loops</h2>
<p>The blog contained a main blog section, along with a “Favorites” section in the sidebar, that would be used to list anything: favorite websites, books, etc. I didn’t think that using just the basic WordPress blogroll or a third party bookmarking site would give the CEO enough flexibility to mark whatever she wanted as a favorite. So I decided to just create a separate category for Favorites.</p>

<p>In the footer of the site, there is also information about the last 6 projects that we had worked on. I definitely did not want those to be static. Since we complete projects almost daily, I wanted “non-technical” people to be able to update them. So I decided to create a separate category for those.</p>

<p>So in total, I had 3 categories: Blog, Favorites, and Projects.</p>

<p>I figured I would just end up having the main loop in the content area, a loop in the sidebar, and a loop in the footer. I had never really done a site with multiple loops, but after reading <a href="http://codex.wordpress.org/The_Loop#Multiple_Loops">the documentation</a>, it didn’t look too complicated. So the key to having multiple loops is to use the <a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts</a> template tag.</p>

<h3>The Main Blog Loop</h3>
<p>The Blog category has an id of 1. So all you have to do is call the query_posts template tag before you loop through the entries:</p>
<div class="codeblock"><code>
&lt;?php query_posts('cat=1');<br /> if (have_posts()) : <br />  while (have_posts()) : the_post(); ?&gt;<br />   &amp;hellip;<br />  &lt;?php endwhile;<br /> endif; ?&gt; 

</code></div>

<p>Then the Favorites and Project categories are basically the same, you just plug in the correct category id into the <strong>query_posts</strong> template tag.</p>

<h3>Paging with Multiple Loops</h3>
<p>So since we are using the <strong>query_posts</strong> template tag, it messes up the paging of the main loop. So, in the main blog loop, we need to use the ternary operator to add in the paging argument:</p>
<div class="codeblock"><code>
&lt;?php<br /> $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;<br /> query_posts("cat=1&amp;paged=$paged");<br />?&gt; 

</code></div>
<p>So those two lines would replace the previous call to the <strong>query_posts</strong> template tag that we used for the main loop.</p>

<h2>Read More Links</h2>
<p>When I had first implemented the blog, we were using the read more links (whenever they were added into the post) for all posts. Later in the implementation process, I was told that the most recent article should always show the full post.</p>
<p>I wasn’t sure how the heck I could do that, but after some Googling I figured it out. Basically what we need to do is just create a simple counter to see if we are on the first post. If we are, we set a global $more variable equal to 1. So I define the $count variable and set it equal to 1, then I have this at the beginning of the main loop:</p>
<div class="codeblock"><code>
&lt;?php<br />if (is_home()) {<br /> global $more;<br /> if($count == 1) $more = 1;<br /> else $more = 0;<br /> $count++;<br />}<br />?&gt; 

</code></div>

<h3>Wait a Minute…</h3>
<p>So I was all ready to publish this article, when I found out today that the first article on my CEO’s blog was still showing the read more link. From what I discovered, it looked like both the <strong>is_home()</strong> and <strong>is_front_page()</strong> conditional tags were not registering as true when on the homepage. For the life of me, I could not figure out why this was happening.</p>
<p>So this was a little bit of a hacky solution, but it was all that I could think of quickly. I had this code before the loop:</p>
<div class="codeblock"><code>
&lt;?php<br /> if($_SERVER['REQUEST_URI'] == '/') { <br />  $isHomepage = true;<br /> }<br />?&gt; 

</code></div>

<p>So essentially, I was setting up my own flag to see if it was the homepage. What that is doing is checking the URL to see if it is the root of the site or not. Then I modifed the if statement in my other code to be:</p>
<div class="codeblock"><code>
&lt;?php<br /> if (isset($isHomepage) &amp;&amp; $isHomepage = true) {<br />  global $more;<br />  if($count == 1) $more = 1;<br />  else $more = 0;<br />  $count++;<br /> }<br />?&gt; 

</code></div>

<p>I’m not sure if there is a better solution, but I think the reason the <strong>is_home()</strong> and <strong>is_front_page()</strong> conditions don’t work is because of calling the <strong>query_posts</strong> template tag, because they worked before the call.</p>

<h2>Custom Fields</h2>
<p>I had used custom fields before, and they are amazingly powerful in being able to extend WordPress functionality. For my CEO’s blog, I used 4 custom fields:</p>
<ul><li>favoriteThumbnail</li>
<li>favoriteUrl</li>
<li>projectThumbnail</li>
<li>projectUrl</li>
</ul><h3>Favorites’ Custom Fields</h3>
<p>The favorites are supposed to be little blurbs about something that my CEO likes, whether it is a book, website, etc. So the idea is that it is not supposed to be a full blog post, just a little blurb. So instead of linking to the WordPress post, we link to the <strong>favoriteUrl</strong> custom field. Here is how the favoriteUrl custom field is being used (this is called within The Loop):</p>
<div class="codeblock"><code>
&lt;?php <br />$favoriteUrl = get_post_meta($post-&gt;ID, "favoriteUrl", true);<br />if($favoriteUrl != '') { ?&gt;<br /> &lt;a href="&lt;?=$favoriteUrl ?&gt;" class="heading"&gt;&lt;strong&gt;&lt;?php the_title(); ?&gt;&lt;/strong&gt;&lt;/a&gt;<br />&lt;?php } else { ?&gt;<br /> &lt;strong class="heading"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;<br />&lt;?php } ?&gt; 

</code></div>

<p>So basically what I am doing there is first calling in the favoriteUrl custom field and assigning it to the $favoriteUrl variable. Then, I check to make sure it is not empty (meaning the custom field was not entered). If it is entered, I output the link to the URL. If not, I still do not link to the WordPress post, I just display the title in bold.</p>

<p>The favoriteThumbnail is pretty self explanatory and is called in the same way as the favoriteUrl. The reason I wanted to use a custom field for the thumbnail instead of just adding the image to the post content is because the design called for the image to be floated next to the title and because I wanted to link the image to the favoriteUrl. This way, when my CEO is adding a favorite, she does not have to worry about floating the image and linking it to the favoriteUrl.</p>

<h3>Projects’ Custom Fields</h3>
<p>Both of the Projects’ custom fields are used similarly to the Favorities’ custom fields. The projectThumbnail custom field is just used to show the most recent 6 projects in the footer. Nothing complicated, just showing and linking the photo to the WordPress post.</p>

<p>Until our new company website gets launched, we are linking to the short WordPress post which gives a little description about the project. The projectUrl outputs the URL to the project at the end of the post. Once our company website is finished, I will just modify the custom field to link directly from the footer to its respective page in our portfolio.</p>

<h2>Conclusion</h2>
<p>I really enjoyed doing this implementation. While it was just a simple blog, it is always great to learn a couple of new tricks/techniques. Any other WordPress tips that you have found useful?</p>