50 | Page Template Sidebar Overwrite Theme Options in Twenty Eleven

g+
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;
      else:
      foreach($wp_classes as $key => $value) {
        if ($value == $val) unset($wp_classes[$key]);
      }
      endif;
    }
  endif;

  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;
      else:
      foreach($wp_classes as $key => $value) {
        if ($value == $val) unset($wp_classes[$key]);
      }
      endif;
    }
  endif;

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.

Finished.

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.

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

  1. Elton comments:

    Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.

    There was a hermit crab inside and it pinched her ear. She never wants to go back!

    LoL I know this is entirely off topic but I had to tell someone!

    Take a look at my webpage – hack line ranger

  2. Domenic comments:

    I’m amazed, I have to admit. Seldom do I come across a
    blog that’s both educative and amusing, and let me tell you, you’ve hit the
    nail on the head. The issue is an issue that too few folks are speaking intelligently about.
    Now i’m very happy I came across this in my search for something regarding this.

    Also visit my webpage; how to get pregnant low sperm count

  3. Kathryn comments:

    What’s Happening i’m new to this, I stumbled upon this
    I have discovered It absolutely helpful and it has helped me
    out loads. I hope to give a contribution & assist
    different users like its aided me. Great job.

    Also visit my webpage – bourse de paris cotations

  4. Tangela comments:

    Your wedding day is all about the two of you and the love you share
    for each other. And yes, they should enter the party at exactly the
    same time for a bigger applause. They can also show you how to use
    different styling aids to make styling faster and easier.

    In fact, some of the best pet pictures have been taken with some of the least expensive cameras.
    I have many sweet things to say to you, but the
    sweetest of them all is that I love you.

    Also visit my web-site … cute animals pictures

  5. Jonnie comments:

    Thanks for the marvelous posting! I truly enjoyed reading it, you may
    be a great author. I will always bookmark your blog and will
    come back in the foreseeable future. I want to encourage continue your great work, have a nice afternoon!

    Here is my site … Facebook Fortune Review

  6. Toni comments:

    Thanks for the auspicious writeup. It in truth was a enjoyment account it.
    Glance complicated to more added agreeable from you!
    However, how could we keep in touch?

    Visit my homepage Hypothyroidism revolution scam

  7. Dieter comments:

    Woah! I’m really enjoying the template/theme
    of this blog. It’s simple, yet effective. A lot of times it’s tough to get that
    “perfect balance” between usability and appearance.
    I must say you’ve done a very good job with this. Additionally, the
    blog loads super fast for me on Safari. Excellent Blog!

    My webpage … cambogia direct Review

  8. skatebarnin comments:

    Thanks Alchymyth, This worked perfectly for me.

  9. 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.

  10. 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

  11. 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=""> <strike> <strong>