Twentyeleven offers a lot of cool features, one of my favorites is the Showcase template. However one of the downsides in my opinion (and probably yours, if you’ve found this article) is lack of a sidebar on your single posts and pages.
Others have pointed out viable solutions, however I wanted to stick to best practices and keep my parent theme unmodified.
Here I will explore how to add the sidebar to a twentyeleven child theme in only half a dozen lines of code.
Note: More information on creating a child theme can be found here.
We’ll begin by adding the sidebar back to your posts, by copying single.php to our child theme and modifying it.
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Add the get_sidebar() function directly above get_footer() like so
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
The same can be done for page.php if you want the sidebar to appear on your pages as well.
Now, the other tutorials I’ve seen advise you to edit twentyeleven’s functions.php and comment out the remove_twentyeleven_body_classes() function and it’s accompanying add_action(). You can certainly do so, but it goes against best practices, and you risk losing your modifications if you update twentyeleven.
In your child theme, create or modify your functions.php to contain the following.
<?php
function remove_twentyeleven_body_classes() {
remove_filter( 'body_class', 'twentyeleven_body_classes' );
}
add_action( 'after_setup_theme', 'remove_twentyeleven_body_classes' );
?>
Here we are removing the filter that is added in twentyeleven’s functions.php, but since the child theme’s files are processed prior to the parents, simply removing the filter is not adequate and would be overridden by the parent function. As a result we need to add an action to remove the filter during the after_setup_theme action.
Save your files, and assuming it wasn’t already, activate the theme.
That’s it. Now you have your sidebar.
Pingback: Move the Navigation Bar in a Twentyeleven Child Theme | iamtgc
Works great! I was wondering how to do this without modifying the twenty eleven parent theme functions.php
Thanks tgc, i’d been working with a twenty eleven child theme to select different page templates but even a renamed template file, which was an exact copy of sidebar-page.php in the child theme folder, wasn’t showing the correct layout (sidebar content was appearing under the page content as if the page was being treated as a single post page – could be something in twentyeleven/inc/theme-options.php?)
Until i found your remove filter action, that is. Safer in the child themes functions.php as well.
Thanks, I did not want to mod my parent function file either took a while to find this though
This worked perfectly! Most other tutorials show how to directly modify the Twenty Eleven theme, but I’m using a child theme.
Pingback: Remove the default header images from a Twentyeleven child theme | iamtgc