60

As posts get their data set up via the_post() (respectively via setup_postdata()) and are therefore accessible through the API (get_the_ID() for e.g.), let's assume that we are looping through a set of users (as setup_userdata() fills the global variables of the currently logged in user and isn't useful for this task) and try to display meta data per user: &...


40

In fact you can, I have a folder in my theme directory called /partials/ in in that folder I have files such as latest-articles.php, latest-news.php and latest-statements.php and I load these files using get_template_part() like: get_template_part('partials/latest', 'news'); get_template_part('partials/latest', 'articles'); get_template_part('partials/...


27

Some very good introductory answers here. Basically, get_template_part() allows theme developers to set up an order of specificity of template files. Think of it similarly to specificity as it applies to CSS selectors. When designing something, you want to start with the bare minimum of specificity, so that it can be easily overridden in parts of a design ...


25

The hm_get_template_part function by humanmade is extremely good at this and I use it all the time. You call hm_get_template_part( 'template_path', [ 'option' => 'value' ] ); and then inside your template, you use $template_args['option']; to return the value. It does caching and everything, though you can take that out if you like. You can even ...


14

I was looking around and have found a variety of answers. Its seems at a native level, Wordpress does allow for variables to be accessed in Template parts. I did find that using the include coupled with locate_template did allow for variables scope to be accessible in the file. include(locate_template('your-template-name.php'));


13

A neat solution found in the codex So if you are looping thru custom posts, you can do this: foreach ($custom_posts as $custom_post) { set_query_var( 'my_post', $custom_post ); get_template_part( 'content', 'part' ); } And in that template itself, you will automatically get a $my_post.


13

Try this function get_products($atts) { ob_start(); get_template_part('block-products-inline'); return ob_get_clean(); } add_shortcode('products', 'get_products'); Little explanation php just outputs your content right away when its see print statement. What we do here is, we are holding all the output in buffer and not giving it in print until the ...


10

In my opinion three or four calls of get_template_part() is perfectly acceptable and even within the norm for some of the larger and more complex themes out there. I don't know how many is too many but I think it would have to be a pretty large number to notice a hit in performance. A Deeper Look get_template_part() calls locate_template(). ...


10

get_template_part is a theme function. You can't load plugin files with that function. Take a look at the source and you will notice the work is done by locate_template. Look at that source and you will see that it always loads from theme directories. However much you may want to use get_template_part it is the wrong function. You will need to include your ...


8

An alternate method: open/close the loop, and then use loop-books.php to contain just the markup for the loop content. e.g.: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Get books query $books = new WP_Query(array( 'post_type' => 'wiki', 'posts_per_page' => '50', 'paged' => $paged )); // Open books loop if ...


7

D'oh, it just needs a global as its inside a function. messages.php: <?php global $message; echo $message; ?>


7

// you can use any value including objects. set_query_var( 'var_name_to_be_used_later', 'Value to be retrieved later' ); //Basically set_query_var uses PHP extract() function to do the magic. then later in the template. var_dump($var_name_to_be_used_later); //will print "Value to be retrieved later" I recommend to read about PHP Extract() function.


7

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


7

Background Unfortunately get_template_part() function doesn't have any suitable filter to achieve what you want. It's possible to use the get_template_part_{$slug} action hook to inject template parts, however, without any change to your theme or a child theme, the original template part will be added anyway. So this way you'll not be able to replace ...


6

As of WordPress 3.4, calls to wp_enqueue_script() can be made inline. So, it is perfectly acceptable to call it where needed, such as inside a shortcode callback, or conditionally based upon returned queries, as you've done here.


6

@s_ha_dum is correct that get_template_part is a theme function, but he is incorrect that plugins are not intended to be extended in this way. It is simply more complicated. This post by Pippin, describes how to use a function that will do the work of loading your plugin templates, while allowing users to override your plugin templates within their theme . ...


6

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


6

get_template_part() includes the PHP file, which will break $resp. You need to use output buffering to capture the output into a variable: ob_start(); get_template_part( 'templates/update', 'profile' ); $data = ob_get_clean(); $resp = array( 'success' => true, 'data' => $data );


5

You will either need to globalize $books (if you want to stick to get_template_part() ) or use require( locate_template( 'loop-books.php' ) ); instead of get_template_part( 'loop', 'books' );. This issue is caused by $books in loop-books.php being defined only in the scope of get_template_part().


5

I'm afraid not. If in codex isn't something you would like to know, try to follow the link to the source and have a look yourself to the code and try to manage it out. I had a look and the get_template_part function is defined as below: function get_template_part( $slug, $name = null ) { do_action( "get_template_part_{$slug}", $slug, $name ); $...


5

This is not a true solution to your problem (checking which template loaded another), but it will work to test if the footer has been loaded or not, and thus if it's loading your partial: if ( did_action( 'get_footer' ) ) echo 'footer-class';


4

Your functions.php doesn’t create output, so you should use locate_template(). Example: locate_template( 'php/functions.nav-menu.php', TRUE, TRUE ); You’ll find this function in wp-includes/theme.php. The first parameter is the file path relative to the theme root, the second tells WordPress to load it (or not), and the third to load it just once. Now a ...


4

Source You can use locate_template to find the template file to include. Bit cleaner than using globals. Ie require(locate_template('messages'));


4

You can define a global variable in your theme functions.php global $mywidgets; $mywidgets = false; in your template part add global $mywidgets; $mywidgets = true; and in your functions you can check if its set to true: function my_function(){ global $mywidgets; $mywidgets = true; if ($mywidgets){ // YES Your template part is loaded ...


4

Just my two cents for future references, a workaround at least in Wordpress 3.5 is to add the variable to $wp_query->query_vars. I needed my global _vk_errors inside a template part and just did $wp_query->query_vars['_vk_errors'] = $_vk_errors; before calling get_template_part().


4

bit of a facepalm, because the answer is in pure PHP $path_parts = pathinfo(__FILE__); //var_dump($path_parts); echo $path_parts['filename'];


4

What you've done will include content-competitions-<post-format>.php. I don't know if that is what you want, but that is correct as far as structure goes. I also don't know what you mean by "to change that one line", but get_template_part will use the two parameters to construct a file name/path so, yes, you do need to include all of it. Take a look ...


4

I think you forgot to check if $the_query have posts. <?php $args = array( 'post_type' => 'testimonials', 'posts_per_page' => 1, 'orderby' => 'rand' ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?...


4

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


4

WordPress caches the user information. There's really no problem just to call get_userdata every time you need it. The only thing you are doing by transfering it to a variable $userdata is have WP fetch it from another place in the memory. Note: read this post for a more general dealing with passing variables to partials.


Only top voted, non community-wiki answers of a minimum length are eligible