When I published my previous post ‘Make Your Own WordPress Sidebars‘, I was asked by many readers if I know how to add custom sidebars for each category which can enable to display the advertisement or widgets related to that particular category under which the post is published.
So first of all, I created different sidebars on my own blog and through this post, I will guide you how you too can add custom sidebars for each category on your blog.
This is really powerful for blogs as you can display different advertisements related to different categories and you can also have different optin-form for each category. You are clever enough to understand the benefits of it so I would not divulge myself much in to it.
So let’s start…How To Add Custom Sidebars For Each Category In WordPress?
You will have to work with codes and if you are not a techy guy, you would want to follow the steps carefully. After all I am always ready to help through comments section. Feel free to use it and let me know if you get any problem.
Step 1: Create or define a new widget
You will be working with functions.php file in this step. Open your theme’s functions.php file
Copy and paste the following code:
// Add a Wordpress category sidebar
register_sidebar(array(
'name' => 'Wordpress Sidebar',
'id' => 'wordpress-side',
'before_widget' => '<div class="sidebar-widget">',
'after_widget' => "</div>",
'before_title' => "<h3>",
'after_title' => "</h3>"
));
OR
// Add a Single Top sidebar above each post content
register_sidebar(array(
'name' => 'Single Top',
'id' => 'single-top',
'before_widget' => '<div>',
'after_widget' => "</div>",
'before_title' => "<h3>",
'after_title' => "</h3>"
));
The first code has class=”sidebar-widget” which is the class for sidebar widget in my theme. For your current blog theme, you will have to find it from your functions.php file from your default widgets code. Most of the time, it is as mentioned in the first code. Class is used just to style the widget and take the parameters as pre-defined in the theme. No other specific reason.
The second code do not have any <div> code purely because I am not going to use it on the sidebar but I want to use that widget at the top of the post. I can use it to drive traffic to my other post as I am currently doing it as shown in the pic below or I can as well use any form of advertisement.
Please note both the above codes will create new widget. To check the newly created widget, go to Appearance > Widget section.
The widgets will be named as WordPress Sidebar and Single Top respectively.
Now that the widgets are created, we have to put the codes to display those widgets wherever we want them to display.
Step 2 : Display Widgets Wherever Needed
You will be working with single.php file in this step.
We are using single.php because this is the file which is called while displaying a single post on your blog.
First let’s define the widget “Single Top” and to do so, find the line below in your single.php
<div id="content-area">
Exactly above that code, put this code as mentioned below:
<?php if ( is_active_sidebar('single-top') ) {
echo '<div id="single-top">' ;
dynamic_sidebar('single-top');
echo '</div>' ;
} ?>
This code will only work, if you have put something in your widget. I mean a text widget or anything in that widget. If the widget is empty then this will not be shown. So while testing this widget, ensure you put any widget within the ‘Single Top’ widget in Appearance > Widgets.
I have used above arrangement to display the banner as shown above.
Now let’s come to the main point. How to add custom sidebars depending on the category under which the post is published.
Find a code get_sidebar(); in single.php file
Replace get_sidebar(); with the following code:
<?php
if ( is_category(wordpress) || ( is_single() && in_category(wordpress) ) ) {
get_sidebar('wordpress'); //sidebar-wordpress.php
} elseif ( is_category('seo') || ( is_single() && in_category('seo') ) ) {
get_sidebar('seo'); //sidebar-seo.php
} else {
get_sidebar();
}
?>
In the above code, wordpress and seo are categories of my site, hence I have used them accordingly. You can use the name of your category, slug or category ID. I prefer to use the slug of a category which is easy to find in the categories page.
Also shown above in the code is
get_sidebar('wordpress')' //sidebar-wordpress.php
This indicates that a sidebar-wordpress.php file will be called whenever a get-sidebar(‘wordpress’) is being read by the server or browser.
So we will have to create a sidebar-wordpress.php file in the root folder of your website. If you have no idea about root folder, then please check this page first before proceeding ahead.
If you have created a widget by the name ‘WordPress Sidebar’ then you will use the code exactly as mentioned below or else you will change the ‘WordPress Sidebar’ to the name of the widget that you defined in your functions.php file earlier.
<div id="sidebar-wrapper">
<div id="side">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Wordpress Sidebar')): endif; ?>
</div><!--side-->
</div><!--sidebar-wrapper-->
The ‘sidebar-wrapper’ and “side” divisions mentioned above are specific to my theme. You will have to find out what your theme uses from the sidebar.php file
My theme’s sidebar.php starts with
<div id="sidebar-wrapper">
hence I have used the code above. Moreover, the sidebar code starts with
<div id="side">
so I have to put the main code within the id=”side” division.
Oh yeah! By the way, I discovered that there is also a FREE plugin which will do what I have just explained but it could be techy also. The plugin is available
here
I know this technical things are a bit fizzy at times but I hope I am trying to make myself clear. If not, you always have the option to raise anything through the comments section or if you want me to create the sidebars for your website categories, then I will help you do so for a small charge of $15 but I would suggest you to try first before ordering.
This is a very powerful technique to get multiple streams of income and multiple ways to capture leads from the same website. Let me know in the comments how it goes for you.
Post a Comment