Dev Notes

Software Development Resources by David Egan.

Custom Sidebars in Roots


Roots, WordPress
David Egan

Custom Sidebars

Register a custom widget area in lib/widgets.php

<?php
/**
 * Register sidebars and widget Areas
 */
function roots_widgets_init() {
  // Primary Sidebar
  register_sidebar(array(
    'name'          => __('Primary', 'roots'),
    'id'            => 'sidebar-primary',
    'before_widget' => '<section class="widget %1$s %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h3>',
    'after_title'   => '</h3>',
  ));

  //Secondary Sidebar for courses CPTs
  register_sidebar(array(
    'name'          => __('Course Sidebar', 'roots'),
    'id'            => 'sidebar-courses',
    'before_widget' => '<section class="widget %1$s %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h3>',
    'after_title'   => '</h3>',
  ));

  //Footer
  register_sidebar(array(
    'name'          => __('Footer', 'roots'),
    'id'            => 'sidebar-footer',
    'before_widget' => '<section class="widget %1$s %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h3>',
    'after_title'   => '</h3>',
  ));

  // Widgets
  register_widget('Roots_Vcard_Widget');
}
add_action('widgets_init', 'roots_widgets_init');

Amend templates/sidebar.php so that the custom sidebar is delivered for the appropriate content types. In this example, the custom sidebar id “sidebar-courses” is associated with the sfwd-courses CPT:

<?php  
if ( is_singular('sfwd-courses') ) :
  dynamic_sidebar('sidebar-courses');
else:
  ?><div class="bottom-pad"><?php get_search_form(); ?></div><?php
  dynamic_sidebar('sidebar-primary');
endif;  
?>
<?php dynamic_sidebar('sidebar-secondary'); ?>

comments powered by Disqus