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

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

  1. Michelle comments:

    In the event you want a keyboard to use in reside performances AND a
    full-featured MIDI controller, the Novation Launchkey
    would possibly just be what you’re on the lookout for. Some folks
    may be in search of a keyboard that lets them try out the fundamentals of music manufacturing,
    while professionals, on the other hand, could also be looking to put money into a
    full-featured keyboard to make use of of their dwell performances.

    While quite a lot of what you need shall be provided by this system itself, the very best
    worth midi keyboard ought to at least embody the likes of pitch and modulation management and a few separate contact pads for
    beat programming. Although designed for a musician who is
    gigging and needs high quality tone and touch piano, the Yamaha
    P-155 can be used at residence for practicing. The keys are stable and semi-weighted, and it comes with only five sounds-piano, electric piano virtual keyboard (http://www.alphaschool.in), organ, synth and bass—that nearly sound like the real
    factor.

  2. Sandy comments:

    Many alternative telephones and PDA’s suppor the Bluetooth Virtual Keyboard.

    The big drawcard for these controller keyboards is the very
    fact the on-board LCD display integrates with software program that
    may ship all your VST instrument presets to the keyboard so that you
    can browse and play (as if inbuilt to the keyboard).
    If the merchandise is there, your Pc has Bluetooth
    hardware put in, and you may safely assume that the software program has been arrange as well.
    If your Pc didn’t come with the Bluetooth hardware installed, you may simply add it by purchasing a Bluetooth USB dongle.
    Like everything else in your computer, Bluetooth requires both hardware and software.

    Permission is granted to make use of this software program for personal and business purposes.
    Does the dongle stay on the USB (176.32.230.27) port every time the Bluetooth function is
    in use? The “dongle” is a hardware gadget that plugs right into a USB port.
    2. Choose Hardware and Sound, and then select Device Manager.

  3. 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)

  4. skatebarnin comments:

    Thanks Alchymyth, This worked perfectly for me.

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

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

  7. 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 to Michelle Cancel 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>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.