Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Last active March 4, 2020 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BinaryMoon/d9bb63283067a129170871a133dae206 to your computer and use it in GitHub Desktop.
Save BinaryMoon/d9bb63283067a129170871a133dae206 to your computer and use it in GitHub Desktop.
Register WordPress sidebars demo
<?php
/**
* Intitiate sidebars
*
* @link https://developer.wordpress.org/reference/functions/register_sidebar/
*/
function mytheme_widgets_init() {
// Sidebar.
register_sidebar(
array(
'name' => esc_html__( 'Sidebar Main', 'mytheme' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Widgets that display on the side of your website', 'mytheme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s"><div class="widget-wrap">',
'after_widget' => '</div></section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'mytheme_widgets_init' );
<?php
/**
* Sidebar Template
*
* Display the sidebar widgets. Will not output anything if there's no widgets
* assigned to specified sidebar.
*
* @link https://developer.wordpress.org/themes/template-files-section/partial-and-miscellaneous-template-files/#sidebar-php
*/
if ( is_active_sidebar( 'sidebar-1' ) ) {
?>
<!-- Sidebar Main (1) -->
<aside class="sidebar sidebar-main" role="complementary">
<?php
do_action( 'before_sidebar' );
dynamic_sidebar( 'sidebar-1' );
?>
</aside>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment