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
52
votes
9answers
60k 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 ...
49
votes
6answers
39k 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. ...
25
votes
3answers
21k 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 ...
25
votes
3answers
64k views

How to use get_template_part()?

Could someone please explain to me how this function works? I know what it does but when I look at the source code in the twenty_ten template, I don't get how all loops are being collected in one ...
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 ...
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 ...
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 ...
9
votes
3answers
21k 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 ...
9
votes
2answers
10k 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' => '...
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 ...
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 ...
6
votes
2answers
12k views

How to make get_template_part always check child theme first?

How can I force WP to always check the child theme folder first when running get_template_part? Example: child theme loads get_template_part('content', 'inventory') in single.php. Because all child ...
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() &...
6
votes
4answers
6k views

When is get_template_part() preferable to simply using the template.php files?

In this post on wordpress.stackexchange.com I asked whether using get_template_part(), as demonstrated in the TwentyTen theme, is a recommended best practice. The general consensus I got was that it ...
4
votes
3answers
4k views

Variable use in get_template_part

As far as I understand (from the get_template_part codex) get_template_part is just a wrapper round the PHP require function. So if I have a variable that I have create in a page template file e.g. $...
4
votes
1answer
688 views

Is it safe/recommended to use wp_enqueue_script function outside the functions.php file?

I created multiple post formats, and when i use an audio post format, i want to include some additional javascript for the audio player. So, i include the content-audio.php like this: <?php ...
4
votes
1answer
54 views

Get the php template file from other theme folder

How to get the php template file from other theme folder? For Example : My Current Theme Folder is promax . Im working on archive.php and want to include a template file inside of archive.php from ...
4
votes
2answers
5k views

get_template_part from plugin

This is default get_template_part function inside WordPress: function get_template_part( $slug, $name = null ) { do_action( "get_template_part_{$slug}", $slug, $name ); $templates = array(); ...
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> &...
3
votes
1answer
13k views

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

Say for example you have a directory like this: theme - subfolder - template.php content-job-listing.php If I try and call get_template_part like so get_template_part('content', 'job-listing')...
3
votes
1answer
4k views

passing argument to get_template_part() or a better way to code

I have this include … event-list.php <?php /** * The loop that displays upcoming events */ ?> <ul class="event-items"> <?php $yesterday = time() - 24*60*60; $args = array( ...
3
votes
2answers
2k views

Single Page Wordpress Theme - Using page templates

Well I am lost, its late and I have been searching google all night. Heres the problem I am running into. I am trying to create a singe page website using wordpress "pages". How I have initially setup ...
3
votes
2answers
2k views

get_template_part inside get_template_part?

Ever tried this? // template file do_action( 'my_hook' ); // ex. functions.php function my_hooked_template_part() { get_template_part( 'my_loop_part_file', 'default' ); } add_action( 'my_hook', '...
2
votes
3answers
754 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 ...
2
votes
2answers
2k views

Loading page content into a variable in template

On one of my custom templates, I need to do some work with the content before displaying it. Is there a way that I can load this content into a variable rather than just outputting it to the page? ...
2
votes
2answers
943 views

Template tags vs get_template_part() vs functions.php

When to use template tags and when to use get_template_part() and when to use function.php? I am confused at them. For example, if I want to show related portfolios under single portfolio, I can ...
2
votes
1answer
2k views

get_template_part not working with ajax

I loaded some contents by ajax, When i try something like this: $resp = array( 'success' => true, 'data' => 'the content here' ); it's working without any issues but if i used ...
2
votes
1answer
3k views

Passing variables to template parts

I have one template file videos.php which has the following line of code in it (aswell as a load of HTML): <?php get_template_part('loop', 'feed-videos' ); ?> inside that template part, I have ...
2
votes
1answer
11k views

Correct way to use get_template_part() and get_post_format() with custom post types?

I have a custom post type of 'competitions'. In order to customise the display for this CPT in, for example, category listings I am doing the following: Create a category-competitions.php file Create ...
2
votes
2answers
1k views

How to move style from template file to <head> section?

Imagine the following situation: //header.php contains: <header id="header">logo and navigation</header> //my-template.php contains: <?php get_header(); ?> <style type="...
2
votes
1answer
693 views

Possible to use pagename inside get_template_part?

I'd like to grab the current page name and pull that specific template part per page. This way I can just set up a generic tier-1 template that calls in a secondary section based on the current page's ...
2
votes
1answer
90 views

Is there a tirck in the get_xxxx function in the general_template.php file?

I'm a newbie in WordPress and I need a big help to understand how do-action works. It seems there is a relationship between the instruction do_action and the code that follows it in the get_xxx ...
2
votes
2answers
3k views

How to pass variables with get_template_part?

I am developing a custom theme and i like to pass some variables to selected files. I am calling the view files from functions.php. $var1 = ; $var2 = ; etc include_once('form-views/view-profile.php')...
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 ...
2
votes
1answer
786 views

get_template_part in admin

I am trying to use get_template_part when creating a page in the admin section of my site. However, it does not seem to be working. Does this function not work in the admin section? What alternative ...
2
votes
1answer
539 views

get_template_part execute with ajax

I would like to get template part with AJAX. This template part - more-images.php contain of source some wp php and html. I read AJAX is build in WP. So I put code below to my theme files. My code: ...
2
votes
2answers
80 views

Finding Page Template and Displaying Content

So, I'm building a theme for a client with one page layout functionality. Currently, I'm allowing the user to create a parent page, and the modules within that page are separate pages that will be set ...
2
votes
1answer
92 views

Problem with using get_template_part() in footer

First off let me just say, I'm new to theme development, I hope there isn't something obvious I'm missing. I'm trying to get this form contactform.php <form method="POST" action=""> <...
2
votes
1answer
50 views

how to remove get_template_part from a plugin? [closed]

I have a template file and that file has two get_template_part(). ie,get_template_part( 'store' ); get_template_part( 'header' ); these two are used inside a template page, I wanted here to ...
2
votes
0answers
27 views

Unable to include a template

On the home.php, which is in the same folder where style.css is kept I am try to call a template: <?php get_template_part( 'template-parts/content', 'home-default' ); ?> template-parts is a ...
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
1k views

Too many get_template_parts?

For a lot of my site I'm using get_template_part mostly because I had coded most of it in html/css first and it just seemed easier to call each part as I saw fit. My question is, basically, will this ...
1
vote
1answer
310 views

How to make get_theme_mod work with AJAX in the customizer preview

Is it possible to make the get_theme_mod work after refresh in the customizer preview pane when it's being called inside a template part by AJAX? I have a theme that loads the posts on scroll (...
1
vote
2answers
307 views

Does get_template_part pull data once in a loop?

When i use the "get_template_part();" inside a loop, does it search for that template file every cycle of the loop (each post) or does it search for the file once and then reuse it every cycle of the ...
1
vote
3answers
236 views

Some doubts about how to show posts in a custom theme?

I am pretty new in WordPress theme development and I have some doubt about this WP function used to show my post in the homepage: <?php if ( have_posts() ) : // Start the ...
1
vote
1answer
1k views

Checking whether template part is loaded in a page

I have a template part like this: <nav id="subpagemenu" class="sidebar"> <?php get_template_part( 'nav', 'subset' ); ?> <?php get_sidebar(); ?> </nav> Both of these ...
1
vote
2answers
1k views

get_template_part() - post-meta not working?

Maybe I simply do not understand what get_template_part() is doing … I have a file called event-list.php that should work as template for other pages and files so all my events (a custom post type) ...
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 =...
1
vote
3answers
146 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(...
1
vote
1answer
1k views

Post Format single-loop.php using get_template_part

I have set up to use post formats gallery and video in addition to the standard. I am editing the loop-single.php to give it different layouts for each post format, but I'm to include ...