Ezeewp – making wordress easy for you
contact us contact us contact us contact us contact us

"Developing WordPress themes, templates and WordPress plugins"

Totally dynamic WordPress sidebars

Thursday 31st December 2009 by admin

0

COMMENTS

Totally dynamic WordPress sidebarsMany of us are blogging on different subjects, usually we call them categories, ours are Blogging, Functions & tips, Social media etc.

How often do we want different content in the sidebar/s. Content that relates to a specific category? This question was also raised by a client – Want WordPress, but want different content in the sidebars.

OK. no problem here is the way to do it. You can create as many totally dynamic sidbars as you want based on categories/ pages even tags. The example below works on categories, creating a sidebar for each category that can hold different content.

Lets look at the standard default function page – function.php open it up and you will see:

  1.  
  2. if ( function_exists(‘register_sidebar’) ) {
  3.         register_sidebar(array(
  4.                 ‘before_widget’ => ‘<li id="%1$s" class="widget %2$s">’,
  5.                 ‘after_widget’ => ‘</li>’,
  6.                 ‘before_title’ => ‘<h2 class="widgettitle">’,
  7.                 ‘after_title’ => ‘</h2>’,
  8.         ));
  9. }
  10.  

Found it, great, this is the WordPress/php function that allows you to put widgets into the sidebar. But did you know you can ‘name’ the sidebars.?

This is an example of extending the WordPress function above for 2 sidebars:

  1.  
  2. if ( function_exists(‘register_sidebar’) ) {
  3.  
  4. register_sidebar(array(‘name’=>’sidebar_no1′,
  5. ‘before_widget’ => ‘<li id="%1$s" class="widget %2$s">’,
  6. ‘after_widget’ => ‘</li>’,
  7. ‘before_title’ => ‘<h2 class="widgettitle">’,
  8. ‘after_title’ => ‘</h2>’,
  9. ));
  10.  
  11. register_sidebar(array(‘name’=>’sidebar_no2′,
  12. ‘before_widget’ => ,
  13. ‘after_widget’ => ,
  14. ‘before_title’ => ,
  15. ‘after_title’ => ,
  16. ));
  17.  
  18. }
  19.  

These functions create the functionalities for 2 different sidebars – sidebar_no1 and sidebar_no2. All that you then need to do is create the sidebar templates.

Open the default sidebar.php and you will see this (line 10)

  1.  
  2.         if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar() ) : ?>
  3. // and at the bottom of the page about line 80 you will see
  4. <?php endif; ?>
  5.  

OK, this works for the standard sidebar template but not for 2 different sidebars – sidebar_no1 and sidebar_no2. To create that functionality you need to change the code on the fist page to:

  1.  
  2. if ( function_exists(‘register_sidebar’) ) {
  3.  
  4. register_sidebar(array(‘name’=>’sidebar_no2′,
  5. ‘before_widget’ => ‘<li id="%1$s" class="widget %2$s">’,
  6. ‘after_widget’ => ‘</li>’,
  7. ‘before_title’ => ‘<h2 class="widgettitle">’,
  8. ‘after_title’ => ‘</h2>’,
  9. ));
  10. }
  11.  

Volia you now have 2 sidebars each of which will allow you to add different widgets.

Only one problem the second sidebar – sidebar_no2 – is not included in your theme templates. Why all this to get so ar but not be able to use the code – well it explains the principles of what comes next.

Lets create totally dynamic side bars for each category in your blog.

This WordPress query allows you to get/obtain all the category names:

  1.  
  2. $qtable1=$wpdb->prefix . "term_taxonomy";
  3. $qtable2=$wpdb->prefix . "terms";
  4. $sql = "SELECT * FROM $qtable1 LEFT JOIN $wpdb->terms ON ($wpdb->terms.term_id) = ($wpdb->term_taxonomy.term_id) WHERE ($wpdb->term_taxonomy.taxonomy = ‘category’) AND ($wpdb->term_taxonomy.parent = ‘1′)";
  5. $categories = $wpdb->get_results($sql);
  6.  

Now we add the sidebar function (above)

  1.  
  2.  
  3. foreach ($categories as $category) :
  4.  
  5. register_sidebar(array(‘name’=>$category->name,
  6. ‘before_widget’ => ‘<li id="%1$s" class="widget %2$s">’,
  7. ‘after_widget’ => ‘</li>’,
  8. ‘before_title’ => ‘<h2 class="widgettitle">’,
  9. ‘after_title’ => ‘</h2>’,
  10. ));
  11. endforeach;
  12.  

In this query $category->name gives you the category name.

Now return to the file sidebar.php and add a bit of code:

  1.  
  2. if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar($categoryname) ) :
  3.  
  4. endif;
  5.  

And at the top of the page use a conditional WordPress query as above

  1.  
  2. $category = get_the_category();
  3. if(empty($category->category_name)):
  4. $categoryname=‘ ‘;
  5. else:
  6. $categoryname=$category->category_name;
  7. endif;
  8.  

By doing that you can display the different sidebars for each category from a single sidebar page.

I have tried to keep this as a simple and easy to follow example without going into too much detail. This functionality can be greatly expanded – as we have done on a number of sites. Easy for the user, easy to administer and gives you great flexability with sidebars.

Once again hope this helps

Popularity: 8% [?]

Please leave a response and trackback from your own site.

Leave a Reply