WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a child theme, I strongly prefer not to override the main template files in order to maintain simplicity of the child theme as well as minimize the amount of code and maintenance over time.

In the loop, the index.php template in parent theme uses: get_template_part( 'content' );

which will bring in content.php, I'm looking to have it behave more like get_template_part( 'content', get_post_format() ); or similar in order to bring in different templates such as content-aside.php, etc, without creating an index.php in the child theme to overwrite the parent for a single line change.

get_template_part() consists of:

function get_template_part( $slug, $name = null ) {

    do_action( "get_template_part_{$slug}", $slug, $name );
    $templates = array();
    $name = (string) $name;
    if ( '' !== $name )
        $templates[] = "{$slug}-{$name}.php";

    $templates[] = "{$slug}.php";
    locate_template($templates, true, false);

So there's an action available, in my case called get_template_part_content

I can hook onto the action with something like:

add_action( 'get_template_part_content', 'child_post_styles', 10, 2 );

function child_post_styles ( $slug, $name ) {
    if( false == $name ){
        $name = get_post_format();
        $templates = array();
        if ( '' !== $name )
            $templates[] = "{$slug}-{$name}.php";

        $templates[] = "{$slug}.php";
        locate_template($templates, true, false);

This obviously results in post duplication, one from my hooked function, which can display the desired template, and the other from the normal locate_template call in get_template_part()

I'm having trouble finding a way to accomplish this without duplication, or without ending page render right after serving my template part by doing something stupid such as a break or exit

share|improve this question
Check this post – Pieter Goosen Jul 8 '14 at 19:00
Welcome to miserable life without template inheritance. :) – Rarst Jul 8 '14 at 19:54
up vote 3 down vote accepted

There is no hook to do this. Copy the index.php to the child theme and make the single line change. Also, tell the parent theme's author that they should make the same change in their theme, for flexibility.

share|improve this answer

I think it's not exactly possible the way you want to achieve. It's really annoying to not getting a filter there to alter template files names :(

I have a workaround on it though. Let use content.php for the comparison.

// content.php file 
$format = get_post_format();
if( '' != $format ){
    get_template_part( 'content-'. $format );
    get_template_part( 'content-default' );

I haven't used the second argument of get_template_part function, rather i passed suffix on the first argument to avoid infinitive calls on content.php file if there's no 'content-'. $format file.

share|improve this answer
I think this is a good way to do that. Not sure why it's not accepted :) – Rilwis Jul 16 '14 at 2:17
Reputation matters, maybe :) – Shazzad Jul 16 '14 at 15:23
Sorry @Shazzad, I gave your answer an upvote though as it is very clever :) If I could accept two, I would. – TomHarrigan Jul 22 '14 at 14:19
Thank you so much for that. – Shazzad Jul 22 '14 at 19:13

If you want to pull in a file called content-aside.php you call the function like this:

get_template_part( 'content', 'aside' );

The second parameter is the name of the filer after the hyphen. If your file is in a sub-directory (templates/content-aside.php):

get_template_part( 'templates/content', 'aside' );

Is that what you're looking to do?

share|improve this answer
Thanks, I understand that. I'm looking to not have to create an index.php in the child theme to overwrite the parent themes index.php for a single line change. It adds more maintenance to the child theme. Hoping to accomplish via a hook if possible. – TomHarrigan Jul 8 '14 at 18:22

Well, there is SOLUTION:

add_action('init', 'x'); function x(){ remove_all_actions('get_template_part_content'); }
add_action('get_template_part_content', 'yourFunc', 99, 2 );

but think carefully, in which hook you should insert that action... also, you can filter content parts, for example:

function yourFunc ( $slug, $name ) {

    .........any codes here.............
    if ($name == 'header'){
       //but this will be executed before loading template... At this moment, Wordpress doesnt support any hooks for before/after LOAD_TEMPLATE...

to manually get location, use, i.e. locate_template('content-header.php', false);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.