Questions tagged [get-template-part]

It is a WordPress core function that loads template parts inside theme templates. It makes it easy for themes to reuse sections of code like pagination, header images, menus, etc.

Filter by
Sorted by
Tagged with
54
votes
10answers
64k views

Passing a variable to get_template_part

The WP Codex says to do this: // You wish to make $my_var available to the template part at `content-part.php` set_query_var( 'my_var', $my_var ); get_template_part( 'content', 'part' ); But how do ...
12
votes
4answers
6k views

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 ...
6
votes
5answers
2k views

How to add custom content template part for a custom post type on main query using a plugin

I have a custom post type paper defined in my WordPress plugin that I show along with standard posts in the main query via: function add_custom_post_types_to_query( $query ) { if ( is_home() &...
2
votes
3answers
767 views

Get template part based on custom taxonomy term

I have a custom post type and I'm trying to call up different variations of the navigation based on the custom taxonomy slug. I've done this fairly easily with normal posts, like so: <?php ...
49
votes
6answers
40k views

Passing variables through locate_template

While I've typically used include or require on their own to save long term code maintenance I've started to use get_template_part and locate_template as using built in WordPress stuff is always best. ...
8
votes
4answers
2k views

Is there a variable for a template parts name?

One you have called a template with get_template_part() (or locate_template()) is there a way to know what template you are in. For example if you call get_template_part('loop','archive'); from ...
3
votes
1answer
9k views

Combining shortcode and get_template_part

I manage a wordpress site and have created a template part that I usually paste below the content. <div class="posts-group"> <h2>Featured store products</h2> &...
9
votes
3answers
22k views

How to include a file using get_template_part() in a plugin?

A very simple question may be, but I'm struggling. In theme development, I worked with get_template_part() many times, and I understand its basics. But when I's developing a plugin, I wondered using ...
1
vote
1answer
103 views

Getting the chosen template when iterating through pages

Im trying print all the pages on a wordpress site. Im using the following code: $args = array( 'post_type' => 'page'); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : ...
25
votes
3answers
22k views

Is there any way to use get_template_part() with folders?

I'm wondering if there is any way to use get_template_part() with folders? My main folder has a lot of files now because I put every re-usable element in a separate file. I'd like to put them in ...
14
votes
3answers
3k views

get_template_part vs action hooks in themes

It seems to me both of these afford the opportunity for the end user to modify a theme without actually editing the themes files (via child themes). My question is, is one method preferred over the ...
9
votes
2answers
11k views

Using a custom WP_Query with get_template_part loop

I have a query for a custom post type: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $books = new WP_Query(array( 'post_type' => 'wiki', 'posts_per_page' => '...
1
vote
2answers
2k views

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 =...
10
votes
1answer
3k views

Should we use get_template_part() in functions files instead of include_once?

I'm using the theme-check plugin to check my theme for errors and recommendations, I'm using get_template_part() in theme files like header.php and index.php but in functions.php I'm using ...
6
votes
4answers
2k views

Check if partial file is called from within header.php or footer.php

I am including a partial file both in header.php and in footer.php with get_template_part('content-form'); Is there an if clause I can use to check from where the file is being called? If it is ...
2
votes
1answer
3k views

Get template part vs locate template function

I'm in single.php where I'm trying to get related-posts.php template. The problem is that when using get template part the related-posts.php isn't getting custom taxonomy name from single.php in ...
1
vote
2answers
70 views

Which custom query am I in and how can I access its properties & methods?

Sometimes there's no way around creating a custom query, but when I do and then loop through it, I often want to use get_template_part() within the custom query loop to pull in a template I already ...
2
votes
2answers
2k views

_wp_page_template to dynamically use template

I'm wondering if it is at all possible to set display a page template chosen by the user within the context of a query_posts using get_template_part. In other words, I have set up a page template to ...
1
vote
2answers
487 views

TwentyTen: Overloading template.php files vs. get_template_part

I'm studying TwentyTen, under the assumption that it contains the best practices for writing themes and modifying them using child themes. I'm noticing what looks like a really redundant practice, ...
1
vote
3answers
191 views

Accessing $post variable from template part

Here is a section of code that I have in a template file — to display sub-navigation if the page has child-pages: <?php // display sub-nav if page has children ?> <?php $children = get_pages(...
0
votes
1answer
16k views

WP_Query on custom post type not displaying, multiple loops & get_template_part

I'm having problems getting my testimonials to display. On page.php I have a testimonials section. So I have a loop that grabs the content for page.php, and then a get_template_part('content', '...