Recently, I had to slice and implement a blog for the CEO of my company. I decided to go with WordPress because that is what I have become enamored with recently, when I created a job board 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.

Multiple Loops

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.

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.

So in total, I had 3 categories: Blog, Favorites, and Projects.

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 the documentation, it didn’t look too complicated. So the key to having multiple loops is to use the query_posts template tag.

The Main Blog Loop

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:

<?php query_posts('cat=1');
 if (have_posts()) : 
  while (have_posts()) : the_post(); ?>
   &hellip;
  <?php endwhile;
 endif; ?> 

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

Paging with Multiple Loops

So since we are using the query_posts 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:

<?php
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=1&paged=$paged");
?> 

So those two lines would replace the previous call to the query_posts template tag that we used for the main loop.

Read More Links

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.

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:

<?php
if (is_home()) {
 global $more;
 if($count == 1) $more = 1;
 else $more = 0;
 $count++;
}
?> 

Wait a Minute…

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 is_home() and is_front_page() 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.

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:

<?php
 if($_SERVER['REQUEST_URI'] == '/') { 
  $isHomepage = true;
 }
?> 

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:

<?php
 if (isset($isHomepage) && $isHomepage = true) {
  global $more;
  if($count == 1) $more = 1;
  else $more = 0;
  $count++;
 }
?> 

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

Custom Fields

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:

  • favoriteThumbnail
  • favoriteUrl
  • projectThumbnail
  • projectUrl

Favorites’ Custom Fields

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 favoriteUrl custom field. Here is how the favoriteUrl custom field is being used (this is called within The Loop):

<?php 
$favoriteUrl = get_post_meta($post->ID, "favoriteUrl", true);
if($favoriteUrl != '') { ?>
 <a href="<?=$favoriteUrl ?>" class="heading"><strong><?php the_title(); ?></strong></a>
<?php } else { ?>
 <strong class="heading"><?php the_title(); ?></a>
<?php } ?> 

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.

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.

Projects’ Custom Fields

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.

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.

Conclusion

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?