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

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 );
}
else{
    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.


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.


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);