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: Mending a small but annoying shortcoming in my blog theme
Hey, thanks heaps for this!
I actually don’t understand why such a great theme would have its single post page look like some sort of landing page. Oh well
I was actually hesitant on using the editor when I first started blogging but it seems to be really unavoidable if you want to make any useful changes to the theme.
Once again, thanks! You make a non-coder’s life really easy!
Hi again!
I hope I’m doing it correctly, I copied the twenty eleven (parent) single.php and page.php files to the child theme directory (through file manager) and now the single and page php files are showing when I go to editor.
You said, “In your child theme, create or modify your functions.php to contain the following.”
How do I create or modify the functions.php file? I tried to copy the functions.php file (through file manager) and i coundn’t load the admin, so i removed it.
Please advise. Thanks.
Hi!
I’m completely new to WP but had a little experiences with blogger. I’m trying to follow your article but am at a complete lost. I’ve managed to create a child theme that includes the style.css file and that’s it.
I copied the whole coding from twenty eleven (parent theme) single.php, functions.php and page.php files and pasted them into editor on child theme. But I think I’m not doing it right as the individual post and pages have no sidebar still. Please help me? I really don’t know where to begin. =(
Thanks,
Karen
Thank you so much for the article, it really helps! The sidebar is working perfectly now. Thanks again! :D
Yay! Between yours and someone else’s tutorials, I got the sidebars to show on single posts and pages. Thanks so much!
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