Multiple Dynamic Sidebars in Wordpress


/ Published in: PHP
Save to your folder(s)

To arrange multiple dynamic sidebars in wordpress, add the following code snippet into functions.php. The sidebar should then appear in the dropdown menu in wordpress under the widgets section. Next, go into your sidebar.php file & edit the dynamic sidebar tag by setting conditionals for when certain sidebars can be used in the sidebar.php file.


Copy this code and paste it in your HTML
  1. // use in functions.php:
  2.  
  3. <?php
  4. // Multiple Sidebars
  5. if ( function_exists('register_sidebar') ) {
  6. register_sidebar(array(
  7. 'name'=>'Page',
  8. 'before_widget' => '<li>',
  9. 'after_widget' => '</li>',
  10. 'before_title' => '<h2>',
  11. 'after_title' => '</h2>',
  12. ));
  13. register_sidebar(array(
  14. 'name'=>'Blog',
  15. 'before_widget' => '<li>',
  16. 'after_widget' => '</li>',
  17. 'before_title' => '<h2>',
  18. 'after_title' => '</h2>',
  19. ));
  20. register_sidebar(array(
  21. 'name'=>'Single',
  22. 'before_widget' => '<li>',
  23. 'after_widget' => '</li>',
  24. 'before_title' => '<h2>',
  25. 'after_title' => '</h2>',
  26. ));
  27. }
  28.  
  29.  
  30. // use in sidebar.php:
  31.  
  32. <?php // Load Dynamic Sidebars
  33. if(!function_exists('dynamic_sidebar')) {
  34. showDefault();
  35. } else {
  36. if(is_front_page()) {
  37. if(!dynamic_sidebar('homepage')) {
  38. showDefault();
  39. }
  40. } else {
  41. if(!dynamic_sidebar('global')) {
  42. showDefault();
  43. }
  44. }
  45. }
  46. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.