How the WordPress sidebar works

The two functions at your disposal are register_sidebar and dynamic_sidebar – despite the name, they don’t need to have anything to do with a “sidebar” in the traditional sense.

As for the term “widget”, this is nothing more than a chunk of code that takes some settings and outputs some content. A user can assign widgets to a registered “sidebar” area in the backend under Appearance > Widgets.

For example, you might have a “header” and “footer” area that your user can edit. Register the two areas like so:

register_sidebar(
    array(
        'name' => 'Header',
        'id'   => 'header',
    )
);

register_sidebar(
    array(
        'name' => 'Footer',
        'id'   => 'footer',
    )
);

And then to display all widgets assigned to “header”:

<?php dynamic_sidebar( 'header' ) ?>

You can go even further by registering “conditional” areas. For example, widgets only for the front page in the sidebar:

register_sidebar(
    array(
        'name'        => 'Sidebar Front Page',
        'description' => 'Widgets that only show in the sidebar on the front page.',
        'id'          => 'sidebar-front-page',
    )
);

And then place the condition around your display function:

<div class="sidebar">

    Some content that's always here

    <?php if ( is_front_page() ) : ?>

        <?php dynamic_sidebar( 'sidebar-front-page' ) ?>

    <?php endif ?>

</div>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)