Get template part vs locate template function

First of all note that get_template_part internally uses locate_template, so your feeling that the latter is slower is wrong. If you look at the code, get_template_part is little more than a wrapper for locate_template, so if one work and the other not, there are 2 possibilities: you are using get_template_part wrong there’s some hook on … Read more

How can I see what template parts are being called for rendering the viewable page?

You can view the template part by using get_template_part($slug) It’s one of those hidden gems inside of WordPress that don’t get the attention they deserve. The get_template_part function is essentially a PHP include or require, on steroids: It already knows where your theme is located and it will look for the requested file in that … Read more

Pass a variable to get_template_part

In category-about.php I have <?php /** * The template for displaying the posts in the About category * */ ?> <!– category-about.php –> <?php get_header(); ?> <?php $args = array( ‘post_type’ => ‘post’, ‘category_name’ => ‘about’, ); ?> <?php // important bit set_query_var( ‘query_category’, $args ); get_template_part(‘template-parts/posts’, ‘loop’); ?> <?php get_footer(); ?> and in template … Read more

Loading page content into a variable in template

You can always use the output buffering to store the printing contents in a variable. function return_get_template_part($slug, $name=null) { ob_start(); get_template_part($slug, $name); $content = ob_get_contents(); ob_end_clean(); return $content; } $content = return_get_template_part(‘content’, ‘page’); This would be most preferable to keep using the get_template_part() right now. An alternative would be to use locate_template() function but it … Read more

Get the php template file from other theme folder

You can filter the template with the template_include filter. This example will filter the archive template for all archive pages. If you need to do this for a specific archive, use is_post_type_archive( $post_types ) as your condition. Put this in your functions.php file of your child theme. function my_archive_filter( $template) { if ( is_archive() ) … Read more

get_template_part() does not work if you call it when you are in a subfolder

get_template_part() will work the same no matter where or how deep you are within your theme. It always includes relative to the theme (or child theme) root. So if you call the following from anywhere: get_template_part( ‘content’, ‘job-listing’ ); … it will try to load (in order): child-theme/content-job-listing.php parent-theme/content-job-listing.php child-theme/content.php parent-theme/content.php To load parts that … Read more