Today I ran into an interesting problem while developing a BuddyPress theme. I was attempting to include a custom sidebar using PHP’s “include” function. The WordPress codex page on “include tags” gives us this:
<?php include( TEMPLATEPATH . ‘/sidebar-index.php’ ); ?>
The problem is that the above code doesn’t always work when you are utilizing a child theme (I am building a child theme based on the default BuddyPress theme). Rather, you should use the “locate_template” hook:
<?php locate_template( array( ‘sidebar-index.php’ ), true ) ?>
What the “locate_template” hook does is searches the child theme’s template folder before searching the parent theme’s template folder.
Pretty nifty, eh?
8 Comments
More posts from themightymo.com
How I created a Local SEO-Driven WordPress website in 1 hour
This morning, I got curious about the competitiveness of a specific keyword in the Google Map (a.k.a. “Local SEO”). The Google map is this strange beast that I am particularly-curious about, because almost 100% of my customers live within a 10-mile bike ride of my house…and I’d like to have more customers so… The keyword…
WordPress Multisite – A Love Story
Rob says WP Multisite should be put out to pasture – I 100% disagree.
Adding Blur to Quicktime Videos in Post-Production is Easy in Youtube’s Free Browser-based Editor!
I recorded a screencast today of me sharing my live “how to” process for creating a new website. The video is about an hour long, and there were a few small things I wanted to blur on the screen (serial numbers for software). I was surprised that Youtube now has a “blur” effect available after…
thanks this works perfect…
thanks for sharing.this is why I like wordpress ,community is awesome!
Hello, I’m facing some problem with this method, I tried to use a hook in child functions.php so I won’t touch files directly, but that didn’t work. Could you drop me a line via mail so I can show you what I’ve done and maybe show me where’s the error.
Marco,
Can you explain a little bit more?
So far, I have only used the “locate_template” hook from a child theme’s page and post template files, so I can’t vouch for how it might work in the functions.php file.
Hello, here’s what I’ve done in functions.php (shouty is the site theme name):
/*define constant*/
define(‘SHOUTY_DIR’, STYLESHEETPATH);
then:
/*here we insert, after the regular sidebar, Shouty’s left sidebar*/
add_action(‘bp_after_sidebar’, ‘shouty_include_sidebar_left’);
function shouty_include_sidebar_left() {
require(SHOUTY_DIR.’/sidebar_left.php’);
}
But this does not load the sidebar_left.php givin an error of file/folder not found in child theme dir, even though is actually there and I can’t figure why. I use this method with frameworks like Hybrid, Thematic and the new Genesis and it always works well.
ps. I tried hardcoding but doesn’t work either.
Marco,
Have you tried using include( TEMPLATEPATH . ‘/sidebar-index.php’ ); rather than define(‘SHOUTY_DIR’, STYLESHEETPATH); ?
Yes I did it at first.
TEMPLATEPATH leads you into the parent theme, giving the error of file/dir not found. Quite the same error as STYLESHEETPATH, this time from the child theme’s dir.
Marco,
Will the path work properly if you include your code in the template itself (i.e. index.php rather than in functions.php)?
Thanks for the tip on inlcude templatepath not working in BuddyPress, I encounter this problem myself. Was looking for a solution. Thanks