1

Ok, the title is surely not good to explain my very basic question, but here it is:

I just started creating a wordpress theme and the index.php has two columns:

1.) The main content and

2.) A vertical column where I am showing other info like Archives, Recent posts all programatically.

Updated

Until now everything was fine then I heared about registering a sidebar that shows some content other than the main content(which I think sounds to me very similar to mine). Is this sidebar a functionality provided by wordpress itself or it has to do something with my theme's sidebar(which was created by me)? Also is a sidebar is necessary to be registered for a theme? Do I need to do something to register my sidebar and if yes what exactly?

Also what exactly does it mean(I mean what happens behind the scenes in terms of DB or or other sql queries) to register a sidebar in a theme?

  • This question is confusing. Can you please try to be more clear about what you are asking? – s_ha_dum Jun 5 '15 at 15:59
  • If you register the sidebar you can use it with widgets and wordpress is aware of it on the back-end, which could help with theme customizations. If you don't register it, then wordpress is not aware of it, and you can't access it as easily through the back-end or through a widget. The codex has some examples -> codex.wordpress.org/Sidebars – gdaniel Jun 5 '15 at 16:07
  • @s_ha_dum: Edited the question. Please let me know it is more clear? – me_digvijay Jun 5 '15 at 16:13
  • Have a look at the documentation for Sidebars. – Milo Jun 5 '15 at 16:21
  • @me-digvijay Any of these help? If so, please up vote or mark as excepted answer. – Brad Dalton Jun 6 '15 at 8:05
1

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>
1

I heard about registering a sidebar that shows some content other than the main content(which I think sounds to me very similar to mine). Is this sidebar a functionality provided by wordpress itself or it has to do something with my theme's sidebar(which was created by me)?

Nothing about this is provided by Core except for the basic sidebar mechanics. The sidebar and any necessary widgets would have to be created by you or installed by you.

Also is a sidebar is necessary to be registered for a theme?

Yes, of course. Otherwise the WordPress and the theme would have no idea that the exist. Technically, a plugin (for example) could register the sidebar but the theme would have to be coded to make use of it.

Do I need to do something to register my sidebar and if yes what exactly?

Also what exactly does it mean(I mean what happens behind the scenes in terms of DB or or other sql queries) to register a sidebar in a theme?

There is sample code in the Codex.

1

This is how the sidebar works in Twenty Twelve

There's 2 parts:

Register the sidebar in your functions file using register_sidebar

function your_widgets_init() {
    register_sidebar( array(
    'name' => __( 'Main Sidebar', 'twentytwelve' ),
    'id' => 'sidebar-1',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => '</aside>',
) );
}
add_action( 'widgets_init', 'your_widgets_init' );

Then display it wherever you like using dynamic_sidebar. In this case its added to the sidebar.php file and included in different templates using get_sidebar(); but you could also call the sidebar directly in any template file using the following code.

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary -->
<?php endif; ?>

Another option is to hook the sidebar in from the functions file using a WordPress or theme specific hook like this:

add_action( 'loop_start', 'custom_sidebar' );
function custom_sidebar() {

    if ( ! is_front_page() )
        return;

    dynamic_sidebar( 'sidebar-1', array(
        'before' => '<div id="secondary" class="widget-area" role="complementary">',
        'after'  => '</div>',
    ) );

}

And to register the sidebar, you could also use this method in your functions file:

 register_sidebar( array(
    'name' => __( 'Main Sidebar', 'twentytwelve' ),
    'id' => 'sidebar-1',
) );

When creating multiple sidebars, you need to use unique I.D's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.