Last week, I introduced the query_posts loop qualifier, and this week I’m going to give you a practical use of the tag. For an example of what we are going for, take a look at Veerle Pieters’ blog.
Notice toward the bottom of the page, you’ll see a section called “Approved”. This is a place for Veerle to put links to cool websites she comes across, without sacrificing real estate on the “main blog”. There are actually 2 ways to do this (maybe more), and I’ll share them both.
Using the Blogroll to Manage a Linkblog
The first method is to use the blogroll to manage your links. To be honest, this is not my favorite method, but it is definitely a bit easier to do. The first thing to do is either use the default “blogroll” category, or create a new category to place your links in. Go to manage > blogroll and start adding links. Once you have your links added (which you can continue to add at any time), then you’ll need to edit your templates. Depending on where you want to add the linkblog on your page, you can either do this in one of the sidebars, or perhaps a blogroll page. For this tutorial, we’ll add it to the sidebar. Here’s the code in the standard widget markup format.
<li>
<h2>LinkBlog</h2>
<ul>
<?php get_links(2, ‘<li>’, ‘</li>’, ‘ – ‘); ?>
</ul>
</li>
Let’s go through this. The “LinkBlog” title can be whatever you want. The get_links tag is what actually calls the links, so let me explain what the stuff in the parenthesis means. The “2” is the category we want to pull from. Go back into your WP admin and go to manage > categories and find either the blogroll category, or the new category you added. Look in the far left column and locate the category ID. Once you find it, go back to your template and put the category ID first in the parenthesis.
Next is the “before and after” tags. For this tutorial, we just want it to be a list, so we use <li> </li> to wrap each link in.
Finally, we see the ” – “, which is nothing more than just a separator between the title and description of the link. You can use anything here such as a <br /> or something else. Whatever you want really. It all depends on how you want it to look.
And there you go, now you have a linkblog but …
There are some limitations
For instance, your description is somewhat limited. Also, there isn’t an easy way to offer an RSS feed for your links (although evidently there is a way). Because of these limitations, I prefer the next method.
Using a blog category and query_posts to manage your linkblog
This starts the same way as the other method. You’ll need to go into manage > categories and add a new category called “Linkblog”. Again, look in the far left column and find the category ID … you’ll need that later.
Next, start writing stories, but be sure to format the content of you post like so:
Nathan Rice’s Blog Herald Page
Nathan’s article archive from the Blog Herald
The reason we do this is because we want people to be able to click the link and go directly to the site. We DO NOT want to link to the linkblog post, as there would be no point to that. It really doesn’t matter what you use as the actual blog post title, as we won’t be displaying that in our linkblog (we’ll only be displaying the_content()). But for your own sake, make sure the title is descriptive so you know what it is.
Related Stories from The Blog Herald
After you’ve added your link stories, it’s time to edit the template. Again, we’ll be working with a sidebar, and again we’ll be using standard widget styling structure. Here’s the code, then we’ll go through it:
<li>
<h2>LinkBlog</h2>
<ul>
<!–the loop–>
<?php query_posts(‘cat=8’); //Display posts only from category 8 ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><?php the_content(); ?></li>
<?php endwhile; endif; ?><!–do not delete–>
</ul>
</li>
Just like before, the first few lines are just structured markup … nothing to explain there. But you’ll notice on line 5, we invoke the query_posts qualifier, and as is pretty self-evident, we are using it to display only the posts from category 8 (which may be different for you depending on the category ID you got when you created your linkblog category).
Immediately after the query_posts, we started the loop. Inside the loop, we wrapped the_content() in list tags so that each time it looped through, we would get a new list item. We also wanted to use the_content() so that we could maintain the formatting present in our post.
After we end the loop, we close the markup elements.
And there you go! You now have a linkblog in your sidebar!
Bonus Tip
You may have noticed that your linkblog entries are showing up in your main section of your blog as well. This can definitely be a problem, since you wanted to have your links showing up ONLY in your sidebar. Here’s how to fix it.
Once again, we’re using query_posts. Find the main loop in the template that controls your homepage (be it home.php or index.php). Right before the main loop, add the following query_posts qualifier:
<?php query_posts(‘cat=-8’); //Exclude posts from category 8 ?>
The minus sign before the 8 tells it to exclude that particular category from the list of stories. Pretty easy, huh?
Bonus Tip 2
If you want to offer your readers an RSS feed of your linkblog, just put a link to the category feed of the linkblog category, like so: http://example.com/category/linkblog/feed/ (assuming you use permalinks).
OK, so now you have 2 easy ways to add a linkblog to your main blog, and offer an RSS feed of it as well. I hope you enjoy!
Happy Coding!