Twenty Eleven – New Page Template with Sidebar Correction

If you are creating a new page template for a child theme of Twenty Eleven, with a sidebar, you need to correct the body_class output to remove the css class .singular.

Add this to functions.php of your child theme:

add_filter('body_class', 'adjust_body_class', 20, 2);  
function adjust_body_class($wp_classes, $extra_classes) { 

if( is_page_template('new-sidebar-page-template-file-name.php') ) : 
// Filter the body classes     
    	
	  foreach($wp_classes as $key => $value) {
	  if ($value == 'singular') unset($wp_classes[$key]);
	  }
      
endif; 
// Add the extra classes back untouched
return array_merge($wp_classes, (array) $extra_classes ); 
}

If you created more than one new page template with sidebar, change this one line in the code to something like:

if( is_page_template('new-sidebar-page-template-file-name.php') || is_page_template('another-sidebar-page-template.php') || is_page_template('further-sidebar-page-template.php') ) :

The above codes are similar to what I used in this recent article.

17 Replies to “Twenty Eleven – New Page Template with Sidebar Correction”

  1. Thanks so much for this. Very helpful! I still prefer this modifying and using this theme over any of the new ones. So to find this is just gold.

    Cheers!

  2. Been scratching my head for a couple of hours trying to figure out why this was happening when my template file was identical. Added your code to my child functions.php file and it has worked a treat! Thanks!

  3. In functions.php, there is a method twentyeleven_body_classes() since Twenty Eleven 1.0.

    This one is adding the css class .singular except for the home page and the page templates showcase and sidebar-page.
    The easiest is therefore to add your own page template as another exceptions. Ex.: I did it for my page-of-posts

    /**
    * Adds two classes to the array of body classes.
    * The first is if the site has only had one author with published posts.
    * The second is if a singular post being displayed
    *
    * @since Twenty Eleven 1.0
    */
    function twentyeleven_body_classes( $classes ) {

    if ( function_exists( ‘is_multi_author’ ) && ! is_multi_author() )
    $classes[] = ‘single-author’;

    if ( is_singular() && ! is_home() && ! is_page_template( ‘showcase.php’ ) && ! is_page_template( ‘sidebar-page.php’ ) && ! is_page_template( ‘page-of-posts.php’ ))
    $classes[] = ‘singular’;

    return $classes;
    }
    add_filter( ‘body_class’, ‘twentyeleven_body_classes’ );

    1. this works, and would be a good option within a cloned, renamed copy of the theme. as it stands, it ignores the importance of creating a child theme. as Twenty Eleven is the default theme of wp3.4, keeping it unedited can be important in case of serious problems. without a child theme, you are also having the risk of accidentally updating your theme and losing the customisations.

  4. Thanks, I got this to do what I want for the single blog post page by changing if( is_page_template('page.php') ) : to if( is_single('') ) :

  5. I want to do this as well. I used this…


    // Add specific CSS class by filter TO REMOVE the body class for the left sidebar

    add_filter('body_class', 'adjust_body_class', 20, 2);
    function adjust_body_class($wp_classes, $extra_classes) {

    if( is_page_template('page.php') ) :
    // Filter the body classes

    foreach($wp_classes as $key => $value) {
    if ($value == 'left-sidebar') unset($wp_classes[$key]);
    }

    endif;

    // Add the extra classes back untouched
    return array_merge($wp_classes, (array) $extra_classes );
    }

    BUT – the body class of left-sidebar is still being put into the site (and yes I made sure I was looking on a ‘page’).

    I want to remove the body class .left-sidebar for the entire site… all possible template files.

    I am working with a 2011 child theme http://ratest3.com/sample-page/

    1. answered in the WordPress.org support forum; possibly use is_page() in the conditional statement. however, there would be other ways of controlling the width of pages without sidebar…
      good luck and cheers, alchymyth

  6. Looking all over for this! Thanks this is much better than the messy CSS override solution I was using.

  7. Thank you. Was just about to go back to 2010!

    What I don’t get is why sidebar-page.php works but if you make a duplicate with a new filename and template name it doesn’t work.

    It shouldn’t be necessary to use the workaround you posted here. I’m really annoyed with WordPress for this one. They must do something similar to what you did somewhere. They need to update their instructions on making new template pages.

    1. I partly agree – it seems that the default theme is not meant to be easy but more a showcase of what is possible, or something to stretch the user’s patience. A read-me instruction manual for the Twenty Eleven would definitvely be a great idea.

Leave a Reply to alchymyth Cancel reply

Your email address will not be published. Required fields are marked *