50 | Page Template Sidebar Overwrite Theme Options in Twenty Eleven

example of left and right sidebars in static pages of Twenty Eleven

Sidebar on the left or on the right within one Twenty Eleven child theme

Twenty Eleven already comes with a page template to add a sidebar to static pages. However, this sidebar will always be on the same side as the sidebar on the front  page, ruled by the ‘theme options’.
On some pages, you might want to show the sidebar on the opposite side.
This freedom of choice is possible with some special page templates and some coding in functions.php of the theme.

The following codes refer to a child theme of Twenty Eleven.

If you don’t want to follow the steps outlined in the Codex, you can download a ready-made child theme from quirm.net.

First step: in your child theme folder, create two new page templates – one for a fixed sidebar on the left, and the other for the sidebar on the right.

To begin with, copy the code of sidebar-page.php, and save it as sidebar-left-page.php and sidebar-right-page.php, resp.

The file names are important as they refer to the same names in the code for functions.php.

Second step: Change the line ‘ * Template Name: Sidebar Template’ to ‘ * Template Name: Left  Sidebar Template’ and ‘ * Template Name: Right Sidebar Template’, resp.; save the files.

Third step: add the following code to functions.php of your child theme:

function twentyeleven_child_pagetemplates_body_classes( $wp_classes, $extra ){

  $classes = array();

  if( is_page_template( 'sidebar-left-page.php' ) ) :
  // correction for the Left Sidebar Template
    $classes[] = 'left-sidebar';
    $blacklist = array('right-sidebar','singular');
    // Filter the body classes
    foreach( $blacklist as $val ) {
      if (!in_array($val, $wp_classes)) : continue;
      foreach($wp_classes as $key => $value) {
        if ($value == $val) unset($wp_classes[$key]);

  if( is_page_template( 'sidebar-right-page.php' ) ) :
  // correction for the Right Sidebar Template
    $classes[] = 'right-sidebar';
    $blacklist = array('left-sidebar','singular');
    // Filter the body classes
    foreach( $blacklist as $val ) {
      if (!in_array($val, $wp_classes)) : continue;
      foreach($wp_classes as $key => $value) {
        if ($value == $val) unset($wp_classes[$key]);

return array_merge($wp_classes, (array) $extra, $classes );

add_filter( 'body_class', 'twentyeleven_child_pagetemplates_body_classes', 20, 2 );

That code will correct the body_classes of the theme, which are central to the layout of your site.


Now you will have two more page templates available in the ‘page attributes’ section when you create or edit a static page.

The sidebar position of these pages will be independant of the theme options (as long as one of the ‘content with sidebar’ options is ticked).

This entry was posted in Tips & Tricks, Twenty Eleven, WordPress and tagged , , , , . Bookmark the permalink.

15 Responses to Page Template Sidebar Overwrite Theme Options in Twenty Eleven

  1. Stacia comments:

    Good day! Do yoou use Twitter? I’d like to follow you if
    that would be ok. I’mundoubtedly enjoying your blog and look forward to
    new updates.

    my website seo link building service

  2. Adela comments:

    Attractive section off content. I just stumbled upon your webllog and
    in accession capital to assert that I acquire actually enjoyed account
    your blog posts. Anyway I will be subscribin to your feeds and even I achievement
    you accezs consistently fast.

    Stop by myy web blog: surrey Builders

  3. Corinne comments:

    Unquestionably believe that which you stated. Your favorite reason seemed to be on the internet the simplest thing to be aware of.
    I say tto you, I certainly get irked while people consider worries
    that they plainly don’t know about. You managed Removals to Spain from UK hit the
    nail upon thee top as well as defined out the whole ting without having side-effects , people could ake
    a signal. Will probably be bacck to get more. Thanks

  4. Hector comments:

    Do yoou have any video of that? I’d care to find out more details.

    Here is my pave :: Espresso Van

  5. Lavada comments:

    Of ϲourse, what a magnificеnt website and instructive posts, I surely will bookmark
    your blog.Hаve an awѕome day!

    Loook at my sіkte … halsa

  6. Betsey comments:

    You should take part in a contest public liability insurance for window cleaners one of the
    most useful siutes on thhe web. I am going to highly recommend this website!

  7. Evelyn comments:

    Wow, marvelous weblog layout! Hoow long have you ever been blogging for?
    youu make runbing a blog look easy. The full glance of your website is excellent, as well as the content material!

    Also viit my web site: web designing courses

  8. Domingo comments:

    As a Green Keeper on a golf course, and landscaper with experience, I’m asked, continually, for help with people’s gardens.
    Do you want to eat outside in your outdoor kitchens
    Arizona. Crushed and made into soft, tumbled
    stone, glass becomes a practical and visually appealing second-hand product.

    Stop by my blog; backyard landscaping ideas (http://toboganium.com)

  9. skatebarnin comments:

    Thanks Alchymyth, This worked perfectly for me.

  10. Monpelaud comments:

    I created a page template as you described above in my child theme of Twenty Eleven.
    It works fine for the position of the sidebar but styles modified in the style.css of my child theme are not taken into account like for the other pages.
    For example margins are not the same as the others pages.
    How can I resolve the issue ?

    Many thanks for your help.

    • alchymyth replies:

      this will depend on what edits you have made to your page styles. the coding does not do any formatting, i.e. any styles should actually not get effected by the code.
      please join the WordPress.org support forum, and ask the question there. you will likely need to post a link to your site, to a page with the problem, to illustrate what you have so far.

  11. Lisa comments:

    Could you please tell me WHERE in the functions.php file I need to insert this code?

    • alchymyth replies:

      you can add the code basically anywhere in functions.php of the child theme – just don’t break the php syntax; safest is possibly just before the closing php tags ?> at the bottom of functions.php

  12. Lorynne comments:

    Thanks for this tut. could you tell me how you could omit the sidebar altogether?So just as you’ve made the templates above with sidebars on the left or right. How could you include a template that had no sidebar at all?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>