Typically in my theme function file I’ll require other files to keep things neat.
require_once("foo.php");
Now working in a child theme I’d like to do that same. I’m adding custom admin options and it seems impossible to include code. I’ve echoed out the path to make sure I’m calling the right file and it is calling the proper location but nothing inside that file seems to run. The code runs fine if placed inside the child theme functions file.
Child themes reference parent themes by directory name, and in a normal install all your themes live in
wp-content/themes/
, so I’d say it’s fine to reference those themes by their relative path:If that makes you uncomfortable, I observe the following constants in WordPress 3.0.1 with a twentyten child theme called tt-child:
So you can do the following in your child theme to reference the parent theme directory:
In a child theme the proper way is
While in the parent theme you can still use
get_template_directory()
still works in the child theme, sadly target the parent theme directory. In your case it’s usefulYou definitely do not want to hard code the URL. The proper way of doing so is
See more info at WordPress Codex
Now, if your e.g. modifying header.php which has an include you would reference it as follows:
Hi @curtismchale:
Don’t know if this is it or not, but you need to include quotes around
foo.php
, like so:Does that solve your problem?
Use
<?php get_template_part( 'name-the-page-here' ); ?>
.Without the extension .php
The simplest and the best way to require files in either the theme’s
functions.php
or in plugins development is by usingdirname(__FILE__)
.In your case just needed this:
If the file you want to require is on another folder,
inc
for example, you would say this:Hope this will help someone in future.
It is completely possible and normal to do includes in
functions.php
.I do it like this in my child theme (
php
is subdirectory for code):If you have issues without apparent reason try enabling debug mode in
wp-config.php
:There might be relevant errors happening, but not displayed.
I think this is the best way to get your child theme path
Get idea from http://dynamicweblab.com/2013/02/get-template-path-in-wordpress-child-themes/