Creating Custom sidebars in WordPress

July 12, 2009 by Andy 





WordPress is a fantastic tool for creating just about any type of website.  As a website grows, you are likely to add new categories to the site, and with new categories comes a problem.

How to show different ads in the sidebars of different categories.

e.g on my diabetes website, I have setup an Amazon feed in the sidebar to pull Amazon products and display them on every page.  This is easy enough to do – if you missed it, read my tutorial on adding Amazon Feeds to WordPress sites.

However, wouldn’t it be nice to show pet specific products on the pet diabetes pages, while showing human diabetes diet books on posts in the “diabetes diet” section?

Well, you can.  Here is how to do it.

1. Add your amazon feed code to a separate “feed file” (the Amazon feed code is the code you inserted directly into the sidebar in the tutorial mentioned above).

2. Setup an if..else structure in the sidebar template to include the relevant feed file depending on the category of the post.

Let’s look at this in detail.

Here is the Amazon Feed file I am going to use as the default code on the site (ie if a specific Amazon feed file has not been specified for a category, this one will be used):

I have saved the file to the folder of my active WordPress template.

I have created a couple more files like this with custom search information:

and..

The naming convention I have used for these files is to add the category ID numbers to the filename so I know which categories these files should be applied to.  Finding the category ID is easy enough.  Login to your Dashboard and go to the category section.  Hover your mouse over a category and look at the status bar of your browser:

All of the feed files need to be uploaded to your WordPress template folder:

With all of the feed files uploaded to my WordPress template folder, I can now go in to the sidebar template and make the necessary changes.

What we are going to do is add an If.. Else structure to test for the category.  This will basically say:

If the post is in category 23, insert the amazon-23 feed file here, else if the post is in category 31, insert the category 31 file and so on.  Let’s see how to do this in PHP.

So, if the post is in category 23 (diabetes diet), show the cookbook related Amazon feed.  If the post is in category 31 (Diabetic Pets), 32 (Diabetic Dogs) or 33 (Diabetic Cats), show the Diabetic pets related feed.  If it’s not in any of these categories, show the default feed (which you can see here on the glycemic index page).

If you are feeling adventurous, read this article on the in_category function.  Towards the bottom of the page they show you a function called “post_is_in_descendant_category”.  By following the instructions on that page you can simplify the code above by basically saying “If the post is in one of the sub categories of Diabetic Pets, include this particular file”.

Here is what that looks like in code:

Basically the highlighted code looks for the post in category 31 or a sub-category of 31 and displays the pet feed if true.

NOTE: To make this work, you need to copy the post_is_in_descendant_category function to your template.  I simply pasted that code at the top of my sidebar template:

Print This Post Print This Post

Related Articles

  • Using WordPress for Affiliate Sites - WordPress is a fantastic tool for creating affiliate sites, but how do you set it all up? We look at a couple of models you could use. Bookmark on Delicious Digg this post Recommend on Facebook Buzz it up Buzz it up share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post
  • WordPress Template Hierarchy - WordPress template system is confusing for those new to WordPress. There is a simple hierarchy of templates that explains why some templates have certain files, while others do not. Bookmark on Delicious Digg this post Recommend on Facebook Buzz it up Buzz it up share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post
  • Adding a Feedburner Subscription Box to your WordPress Site - Feedburner is a great free service (now owned by Google).  It allows people to subscribe to your feeds, and will even email your subscribers for you whenever you post new material to your blog.  It doesn’t matter whether you post 1 or 100 new articles to your site in any given day – Feedburner will
  • Things you need to know about Permalinks - Permalinks are a great way to optimize your blog post URLs.  If you read my article on WordPress Permalinks,  you may be using the same permalink structure that I use, namely this one: /%category%/%postname%/ With this type of permalink structure, your post URLs will look like this: http://affiliate-minder.com/wordpresstutorials/install-wordpress-theme/ This is an actual URL from this site.  See that the
  • Choosing a WordPress Theme - Changing your WordPress theme is very easy, so you can have some fun downloading and changing the look and feel of your site.  There are a few things you should look out for when choosing a theme. Ideally you should have some posts already on your blog, as well as a static page, some comments e.t.c.
"The Money Is In The List"

AWeber proves it to thousands of businesses every day.

Learn how email marketing software
can get you more sales, too.

Comments

2 Responses to “Creating Custom sidebars in WordPress”
  1. Suneel says:

    What!!!

    This should not be this easy man.

    I have spent a day and a night to set common sidebars and you have posted setting up custom sidebars????

    Thanks for writing it in such a detail.

    I am going to try and insert a newsletter form in the sidebar on one of my blogs now. :)

  2. Jeremy Young says:

    Hi Andy, you can also use the widget logic plugin to do a similar thing
    There may be some issues with pasting the php code for your feed into a text widget though
    http://wordpress.org/extend/plugins/widget-logic/

Leave A Comment...