PHP WordPress

How to Customize WordPress Sidebars

If you haven’t noticed! I’ve not only migrated my domain to t.omlinson.com, but I’ve also created a child theme based off of the Redwood WordPress theme. One particular element I needed to add was different WordPress sidebars based on the category page.

Old Way of Customizing Your Sidebars

In the past, I used to buy HTML themes and convert them into WordPress themes. This avoided some of the bloat found within premium themes and required a much smaller investment. During that era, this is how I would ensure different pages could support different sidebars.

I would create a PHP file called “wordpress-sidebar.php.” Inside, I’d insert the customization that I wanted. I’d usually not use widgets because it wasn’t something I was comfortable with at the time. Afterwards, I’d just add an if statement to call on the file I just created wherever I needed to.

The code would look something like this.

<?php 
 
//if you wanted a custom sidebar for your wordpress category archive page
//you could use in_category() if you wanted to target a post within a category
 
if(is_category("wordpress"){ 
 
      //actually not sure if TEMPLATEPATH is necessary, leave a comment if you know!
 
      include(TEMPLATEPATH . '/wordpress-sidebar.php');
 
      // the else would call the default sidebar, you could use elseif to create a chain if you wanted
 
} else {
 
      // this is a command to get the default sidebar in WordPress
      // it is identical to using include 
 
      get_sidebar();
}
?>

Pretty simple, but it’s the old and wrong way to do it.

New & Better Way to Create Different WordPress Sidebars

You begin by entering into your theme or child theme’s “functions.php” file. You’ll want to add code similar to this one:

<?php 
 
// Basic was of registering new WordPress sidebars
 
if ( function_exists ('register_sidebar')) { 
    register_sidebar ('wordpress_sidebar'); 
 
    // More advanced way of adding an alternative sidebar allow you to control some of the style and CSS
    // elements. You could also use a function called register_sidebars()
    //  to create multiple WordPress sidebars but the function is not recommended. 
 
    register_sidebar( array(
        'name' => __( 'Work Sidebar', $theme_slug), //name that appears in your widget area
        'id' => 'work', //id to call your sidebar this is only variable you must specify
        'description' => __( 'Widgets in this area will be shown on all posts and pages.',  $theme_slug ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
	'after_widget'  => '</li>',
	'before_title'  => '<h2 class="widgettitle">',
	'after_title'   => '</h2>',
    ) );
 
// Be sure to replace $theme_slug with your theme's slug! 
 
} 
?>

This will allow you to modify your sidebar using the regular widget section. Next step if similar to the previous way of adding WordPress sidebars, create a file based on the id of your newly registered sidebar. In this case, the file would be named “sidebar-work.php”.

You’ll make whatever modifications you need in that new file and make sure that your dynamic_sidebar(); function refers to your new sidebar by name. If you don’t make changes to that function, your widgets will not show up.

Now, whenever you use the get_sidebar(); function, you can call the work sidebar by doing get_sidebar(‘work’);.

If you need additional help, you should definitely do one of these three things:

  1. Read the WordPress Codex page about customizing your sidebar.
  2. Search the WordPress Development Stack Exchange.
  3. Comment below and I’ll do my best to help you out.

You Might Also Like

No Comments

Leave a Reply