Adding Conditional Sidebars To WordPress

Written on:December 31, 2012
Add One

Conditional Widgets in WordPress

Personally I never wrote any PHP code in my life and have no clue about it. When it comes to websites I generally always manage to figure out how to modify anything to my needs, mostly by using Google. But yesterday I had a very specific issue I couldn’t figure out how to do for hours.

What I wanted was to add additional sidebar zones to my WordPress theme and make them show only if a specific type of post was being viewed, in my case attachment. As mentioned since I don’t know any PHP I had to look up the code and it took me several hours and many, many tries to get this right.

I’m sure that for everyone who knows PHP coding this is basic stuff, but in case you want your widgets in WordPress to show only when viewing posts, pages, attachments or some other pages here’s what you need to do.

Step one: Add new widget area to functions.php

Locate where your widgets are defined in your theme’s functions.php file and add the following code (or modify to your needs:

    register_sidebar( array(
        ’name’ => __( ‘Sidebar banners’, ‘ar2′ ),
        ’id’ => ‘sidebar-banner’,
        ’before_widget’ => ‘<aside id=”%1$s”>’,
        ’after_widget’ => ‘</aside>’,
        ’before_title’ => ‘<h3>’,
        ’after_title’ => ‘</h3>’
    ) );

I was using Arras 2 theme (AR2), but the code should be similar for any other theme. Since all modern themes have widget areas already defined in functions.php file you might even be able to copy/paste the existing code for widgets if it’s not the same. Just make sure you change the “id” to something unique.

This will basically register your sidebar (more here) and enable you to add widgets to it in WordPress. By changing the values for before_widget, after_widget, before_title and after_title you can add custom CSS classes as well.

Step two: Add this to sidebar.php file

Copy and paste the following code to wherever you want your new sidebar widgets to appear (usually above or below existing sidebar):

<?php if (is_attachment() ) : ?>
        <?php if (!function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘sidebar-banners’) ); ?>
           <?php else : ?>
        <?php if (!function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘not-attachment’) ); ?>
<?php endif; ?>

What this code does: if the post being viewed is an attachment (for example image) it will show the “sidebar-banners” widget area and any widgets you placed there in your WordPress widget management page (Appearance > Widgets). On every other page or post type a sidebar called “not-attachment” will be shown (make sure to define it as well as explained in step 1). If you don’t need the”not-attachment” sidebar just delete that line of code.

By changing <?php if (is_attachment() ) : ?> to <?php if (is_page() ) : ?> or <?php if (is_post() ) : ?> you can also show the widget only on pages or posts, respectively. There are a ton of conditional tags WordPress supports, all of which you can find on the Codex. Some examples include:

  • is_front_page() – only shows on front page of your blog
  • is_single() – meaning not “page” type
  • is_single(35) – only shows if post ID in the database equals “35″.
  • is_single(About Me) – shows post with the title “About Me”. Also supports permalink such as (about-me)
  • is_single( array( 17, 19, 1, 11 ) ) – in the same way you can use multiple post ID’s, titles etc.
  • is_page()
  • is_category()
  • is_tag()
  • is_author()
  • is_date()
  • is_search()

There are even more conditional tags available so make sure to visit the page on WordPress Codex to see them all.

Easy Mode: Widget Logic Plugin

In case you need to display certain widgets only on particular pages, you don’t have to dabble in the code at all in fact. There are plenty of plugins available that will do the job remarkably well, such as Widget Logic. You simply define where and how you want your widget to be displayed and it will work perfectly. It even supports PHP code.

Unfortunately in my case I couldn’t use it because I needed to use widgets from another plugin. But luckily WordPress is an extremely flexible system and with a bit of time and Google you can find out how to do just about anything.

My solution is probably not the prettiest, but it’s the best way I could figure out how to do it on my own. I just hope it helps!

Leave a Comment

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>