Title: Dynamically create sidebars?
Last modified: August 19, 2016

---

# Dynamically create sidebars?

 *  [termserv](https://wordpress.org/support/users/termserv/)
 * (@termserv)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/dynamically-create-sidebars/)
 * I am wondering if there is any way to automatically create sidebars for each 
   page? A client of mine has 4-5 pages with content, but want to be able to control
   the content of each sidebar for each page.
 * I could go on and add 5 `register_sidebar` functions for each page, but if the
   client adds a new page, this new page won’t have a sidebar. So I was wondering
   if there is a way to dynamically add sidebars for each page in `functions.php`?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/dynamically-create-sidebars/#post-1294030)
 * Is the client using widgets or are the sidebars in code?
 * Either way, you can wrap your sidebar code in IF statements in sidebar.php Some
   themes have more than one sidebar file.
 *     ```
       if ( is_page( array('about', 'contact', 'another-page', 'a-fourth-page'))) {
       ===== do stuff ===========
       }
       ```
   
 * I realize that code is not dynamic. If you define say 3 different sidebar layouts,
   you could create a custom field on each page called Sidebar, and assign a value
   of Sidebar1, Sidebar2, or Sidebar3.
 * Then your sidebar code would test for the existence of the custom field and display
   the sidebar accordingly. When the client adds new pages, they would create a 
   custom field for the sidebar they wanted to display. Thus it becomes dynamic.
 * In sidebar.php:
 *     ```
       $sidebar = get_post_meta($post->ID, 'Sidebar', true);
   
       if ($sidebar == 'Sidebar1') {
       ==== sidebar 1 display code here ====
       } elseif ($sidebar == 'Sidebar2) {
       ==== sidebar 2 display code here ====
       } elseif ($sidebar == 'Sidebar13) {
       ==== sidebar 3 display code here ====
       } else {
       ========  default condition code here =====
       }
       ```
   
 * They do need you to create new sidebars if an altogether new sidebar layout is
   added, but doing that is beyond the range of most clients anyway.
 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/dynamically-create-sidebars/#post-1294044)
 * > I realize that code is not dynamic. If you define say 3 different sidebar layouts,
   > you could create a custom field on each page called Sidebar, and assign a value
   > of Sidebar1, Sidebar2, or Sidebar3.
 * Done that [here](http://www.boosten.org/example-uses-of-custom-fields-in-wordpress/)
 * I think it’s possible to create a page template that holds the code for checking
   whether a sidebar exists, and if not create one, and use the page ID as sidebar
   ID.
 * Never done it, but maybe you can 🙂
 * Peter
 *  [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * (@pboosten)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/dynamically-create-sidebars/#post-1294049)
 * Just to help you going:
 *     ```
       <ul id="sidebar">
       <?php
       if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar($post->ID)) {
         if ( function_exists('register_sidebar') ) {
           register_sidebar(array(
             'id' => $post->ID,
             'before_widget' => '<li id="%1$s" class="widget %2$s">',
             'after_widget' => '</li>',
             'before_title' => '<h2 class="widgettitle">',
             'after_title' => '</h2>',
           ));
         }
         dynamic_sidebar($post->ID)
       }
       ?>
       </ul>
       ```
   
 * Not tested!
 * Peter

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Dynamically create sidebars?’ is closed to new replies.

## Tags

 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * 3 replies
 * 3 participants
 * Last reply from: [Peter Boosten](https://wordpress.org/support/users/pboosten/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/dynamically-create-sidebars/#post-1294049)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
