Adding category-specific post links to the sidebar.
• Part 1: I Discover that WordPress Makes an Excellent Content Management System
• Part 2: Finding and Modifying Just the Right Theme
• Part 3: Planning the Site’s Organization and Creating the Pages
• Part 4: Adding Category-Specific Post Links to the Sidebar
• Part 5: Adding Random Images
• Part 6: Adding Print and E-Mail Features
• Part 7: Creating a Title-less Home Page
If you’re just tuning in, this is the fourth installment of my series of articles about using WordPress as a CMS to build an informational Web site. Throughout this series, I’m talking about a specific site I developed: Flying M Air, a helicopter tour and charter company I operate when I’m not writing and tweaking my Web sites.
Before I begin this installment, I want to take a moment to remind readers that although the topics I’m covering in this series are for Intermediate to Advanced WordPress users, WordPress 2: Visual QuickStart Guide, which I co-authored with Miraz Jordan, is for Beginning to Intermediate users. The book uses an extremely structured step-by-step approach, with lots of referenced screenshots to guide readers through the completion of hundreds of tasks. (Click here to download a PDF file (2.3 MB) of part of Chapter 3.) This article series uses a more conversational narrative tone and assumes that readers already know the basics.
My point? Don’t shy away from the book if the articles in this series have been too difficult for you to follow. The book will likely provide you with the basic information you need to get started with WordPress and move on to the next step. After working with WordPress (and hopefully, the book) for a while, you’ll be ready to articles like the ones in this series.
One of the drawbacks of using a blog format for sharing information that isn’t necessarily date-specific is that entries are presented in reverse chronological order. To get around that, I had to tweak the dates of the posts so the ones I wanted to appear at the bottom of a category page would have earlier dates than the ones at the top.
The other related problem is that not everyone likes to scroll down a page to see all of its contents. So, for example, if I had six tours in my Wickenburg Tours category, there was a good chance that a site visitor would never scroll down the page to see them all.
To resolve this potential problem, I decided that I wanted a list of all tours to appear near the top of the sidebar. But only the tours for the category currently being viewed. So while viewing the Wickenburg Tours category, I wanted just the entries for that category’s tours to be listed.
To do this, I had to insert some conditional statements into the sidebar.php template file. Here’s an example:
What this basically says is that if WordPress is displaying the Category 2 page, it should display a heading of “All Wickenburg Tours” followed by an unordered list of up to 10 post titles (with links) for category 2 in alphabetical order.
(Keep in mind that this code includes a lot of extra tags required by my theme for formatting lists in the sidebar; these tags must be stripped out or modified for other themes.)
I then created the same bit of code for each category that listed my tours and inserted it into the sidebar.php file, right after the search form.
I went a step further with this, however. I also wanted the appropriate list of posts to appear if a visitor was looking at a post on its own (single) page. So I copied and pasted all the conditional code to the area right above it, changed all the is_category tags in the copy to in_category tags, and inserted before the first conditional tag and after the last one. That set up the tags so they’d be executed properly on a single page, too.
The best way to see what I’m talking about is to just visit the site and experiment with its category and post links.
In the next installment of this series, I’ll explain how I got the images to automatically change in the left sidebar and on many of the pages.
This solution is a step towards CMS-like behavior, but I’ve found it impossible to adapt to the main page. If you drop the conditional expression testing for the presence within a particular category, WP flops! You can’t show lists of category entries using that code. And yet, that’s one of the most desirable capabilities to have.
Even with your solution, tho, it has to be hand-coded instead of managed from the admin area of WP. This is really not on, is it?
I’m not quite sure what you’re trying to do here. In my solution, I was able to test for a specific category and list its entries in the sidebar, thus making a sort of index of entries for a category. It seems to work fine. Do you want a list of entries on the Home page? Is that what you’re trying to do?
Yes, that’s what I’m trying to do: Have links to posts arranged by category in the sidebar on the home page or main temmplate.
Incidentally, your solution worked just fine, just as you said it would.
Thanks.