wordpress sidebar widget code example

Example 1: register sidebar wordpress

<?php if ( is_active_sidebar( 'your-sidebar-slug' ) ) { ?>
    <ul id="sidebar">
        <?php dynamic_sidebar('your-sidebar-slug'); ?>
    </ul>
<?php } ?>

Example 2: register sidebar wordpress

/**
 * Add a sidebar.
 */
function wpdocs_theme_slug_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Main Sidebar', 'textdomain' ),
        'id'            => 'sidebar-1',
        'description'   => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
    ) );
}

Tags:

Php Example