WordPress Sidebars

Sidebars

  • Sidebars are any widgetized area of the screen. Originally named as such b/c they appear as a literal sidebar, they are not limited in location.
    • Must be registered in functions.php
    • Parameters: name (seen in Widgets panel), id (called via dynamic_sidebar()), description (Widgets panel), class (CSS), before_widget (HTML), after_widget (HTML), before_title (HTML), after_title (HTML).
  • function theme_name_widgets_init() {
    register_sidebar( array(
    ‘name’ => _( ‘Primary Sidebar’, ‘theme_name’ );
    ‘id’ => ‘sidebar-1’,
    ‘before_widget’ => ‘<div id=”%1$s” class=”widget %2$s”>’,
    ‘after_widget’ => ‘</div>’,
    ‘before_title’ => ‘<h3 class=”widget-title”>’,
    ‘after_title’ => ‘</h3>’,
    ) );
    }
    add_action( ‘widgets_init’, ‘themename_widgets_init’ );
  • You use either register_sidebar() or register_sidebars() to add widgets.
    • Recommended: Add individually.
  • To show a sidebar, create a sidebar.php file.
    • Use dynamic_sidebar() to load sidebar.
    • Use get_sidebar() to load sidebar in main template.
  • Naming Convention: sidebar-{name}.php
  • Basic Sidebar Code:
    <div id=”sidebar-primary” class=”sidebar”>
    <?php dynamic_sidebar( ‘primary’); ?>
    </div>
  • Note: Takes a single parameter, $index, can be sidebar name or id.
    • <?php get_sidebar($name); ?>
  • Use is_sidebar_active() to display default content in sidebar if user hasn’t customized.