Is it possible to override the result of get_template_part()?

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

Answers 4

  • 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.

  • 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.

  • Well, there is SOLUTION:

    add_action('init', function(){ remove_all_actions('get_template_part_content'); });
    add_action('get_template_part_content', 'yourFunc', 99, 2 );
    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...

    but think carefully, in which hook you should insert that action... to manually get location, use, i.e. locate_template('content-header.php', false);

  • 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?

Related Questions