Paul Underwood
PaulundSoftware Engineer

Create A Widget Area For A Certain Category

In Wordpress you can create a sidebar widget area where the admin user can dynamically choose which widgets are displayed on the website. This makes it very easy for the user to add, edit or delete widgets with a nice drag and drop interface. The problem with most Wordpress themes is that developers only give the functionality to have one widgetised area in the sidebar, this means that the sidebar will be the same on every page. Most users want to display different content depending on the page they are on. In this tutorial we are going to learn how we can create a new Wordpress sidebar widget area for a certain category page. This can then be extended to be used for all categories, certain posts, certain pages etc. To add new sidebars to your Wordpress site you need to change the functions.php file and the sidebar.php of your Wordpress theme.

Creating The New Sidebar

First we need to add code to the functions.php file to create a new sidebar for a certain category. To create a new sidebar you need to add a new function on to the widgets_init action, which is done by adding the following to the functions.php file.

add_action( 'widgets_init', 'create_category_sidebar' );

Then we need to create a new function called create_category_sidebar(), in this we are going to get the category by using the Wordpress function get_category() and pass in the category ID we want to use. The function get_category returns an object of the category information, from this we can get the category name to set the name and the ID of the sidebar. Once we have the category data we use the function register_sidebar() to create a new sidebar.

function create_category_sidebar() {
    $category = get_category( 481 );
    register_sidebar( array(
        'name' => $category->cat_name,
        'id' => sanitize_title($category->cat_name) . '-widget-area',
        'description' => $category->cat_name . ' sidebar area',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );

With this code inside the functions.php file you can now go into the widgets page in the admin area and will see a new sidebar area where you can add new widgets to this sidebar.

Displaying The Sidebar

With the sidebar registered on your site and widgets added inside the sidebar we can now display this new sidebar just when you are on the specific category page. Opening the sidebar.php file and add the following code.

     /* Widgetised Area */   
     if ( !function_exists( 'dynamic_sidebar' ) || !dynamic_sidebar()) 

This code will first check to see if we are on a category page, if we are then it gets the current category name and creates a variable with the same name as the ID used on the register_sidebar() function. We can then use this variable as the parameter for the dynamic_sidebar(), this will then display the sidebar with the ID $cat_name. This will attempt to display a new sidebar on each category page, if a sidebar is registered you will see all the widgets assigned to that sidebar, if the sidebar isn't registered you will not see anything.