Register Custom Sidebars in THEME/functions.php
| 01 | add_action( 'widgets_init', 'my_register_sidebars' ); |
| 02 | |
| 03 | function my_register_sidebars() { |
| 04 | |
| 05 | register_sidebar( array( |
| 06 | 'name' => __( 'Primary Widget Area'), |
| 07 | 'id' => 'primary-widget-area', |
| 08 | 'description' => __( 'The primary widget area' ), |
| 09 | 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', |
| 10 | 'after_widget' => '</li>', |
| 11 | 'before_title' => '<h3 class="widget-title">', |
| 12 | 'after_title' => '</h3>', |
| 13 | ) ); |
| 14 | |
| 15 | register_sidebar(array( |
| 16 | 'id' => 'topwidget', |
| 17 | 'name' => __('Top Widget'), |
| 18 | 'description' => __('Widget area between nav bar and content on index page'), |
| 19 | 'before_widget' => '<li id="%1$s">', |
| 20 | 'after_widget' => '</li>', |
| 21 | 'before_title' => '<h3 class="widget-title">', |
| 22 | 'after_title' => '</h3>' . "\n\t" |
| 23 | )); |
| 24 | register_sidebar(array( |
| 25 | 'id' => 'shop', |
| 26 | 'name' => __('Shop Sidebar'), |
| 27 | 'description' => __('Sidebar for shop custom post type'), |
| 28 | 'before_widget' => '<li id="%1$s">', |
| 29 | 'after_widget' => '</li>', |
| 30 | 'before_title' => '<h3 class="widget-title">', |
| 31 | 'after_title' => '</h3>' . "\n\t" |
| 32 | )); |
| 33 | } |
Create sidebar-mycustom.php.
You will see your sidebars now listed in ‘Appearance’ -> ‘Widgets’.
In your sidebar-templates you may now call the dynamic generated content (widgets) by the name of your sidebars eg in sidebar-mycustom.php:
| 1 | <?php if ( function_exists ( dynamic_sidebar('custom') ) ) : ?> |
| 2 | ... regular html ... |
| 3 | <?php dynamic_sidebar ('custom'); ?> |
| 4 | ... regular html ... |
| 5 | <?php endif; ?> |
Integrate your sidebars in your template-files (e.g. index.php, single.php, archives.php):
Default sidebar:
| 1 | <?php get_sidebar (); ?> |
Custom Sidebar:
| 1 | <?php get_sidebar ('mycustom'); ?> |
Notes:
- sidebar file name does not need to be relates to the registered sidebar name
- registered name is used to call the dynamic sidebar eg: custom
- custom sidebar file is called from template using get_sidebar(‘mycustom’)