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
53
votes
7answers
44k 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. ...
67
votes
13answers
91k 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 ...
2
votes
2answers
4k 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 ...
1
vote
1answer
52 views

Template part inside shortcode, unexpected reult

I am attempting to call a template part via a shortcode. Ive read all about how to do this over and over on this site and yet after having tried a number of things still am unable to call a simple php ...
2
votes
3answers
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')...
3
votes
3answers
5k 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
1answer
593 views

Change the file path on get_template_part via plugin

I'm trying to make a plugin that will change the behavior of a theme. In the theme file I have a get_template_part('libs/templates/user_menu');. I want to make my plugin "force" the ...
1
vote
1answer
869 views

get_template_part based upon post's category

I have a WordPress website setup with 5 main parent categories each with dozens of child categories For each WordPress post in single.php, I'd like to load some content based upon each child category ...
0
votes
1answer
2k views

Can't load search results with ajax

I'm trying to load search results on my page without reloading. I tried multiple approaches already, but I still can't make it work, despite using someone else's confirmed solution. Please find the ...
0
votes
0answers
19 views

get_template_part() break bootstrap row columns layout

I'm having some trouble to get my boostrap layout working in wordpress. I want to show some posts on a single row with a loop that show the content inside a col-8 and another loop that show the ...
0
votes
1answer
13 views

How to pass a variable to get_template_part that's updated every time the template part is called?

I have a loop and in that loop I use a get_template_part to call the HTMl for the post to display. I also have a variable (number) to which +1 is added every time the loop runs. In this way I can ...
0
votes
0answers
12 views

When to use get_template_part versus a function

What are good guidelines on when to use get_template_part() versus code in functions? I have both throughout my theme, and I think I've used them correctly, but I'm suddenly not sure I can articulate ...
0
votes
1answer
26 views

Adding a second loop breaks everything

I have a page template that has the following: <?php get_template_part( 'include', 'certifications-popular' ); ?> Within this file, I have two loops. But having the second loop in there seems ...
0
votes
0answers
42 views

Ajax and get_template_part

I found this https://stackoverflow.com/questions/33999193/wordpress-load-more-posts-onclick-with-ajax-request-jquery to load posts via Ajax. It works great except that I would like to use a template ...
0
votes
0answers
20 views

how to use a different template (index.php) in wordpress

I want to choose the best way to separate the mobile template and the desktop. Is this the right way? If I put this code in the index.php file: <?php if ( wp_is_mobile() ) : ?> <?php ...
0
votes
2answers
58 views

get_template_part for template in subdirectory not working

I'm working on my first wordpress site, and moving it online. I have been adding some extra templates to design a simple booking engine, that was working perfectly on my offline localhost server. ...
0
votes
1answer
29 views

Is_Page doesnt detect my page

I want to call Servicedeneme.php which is in main folder of theme, in mysite.com/faq/ It's title is FAQ so I write code for two form which are faq and FAQ.However, it doesnt work. I write below code ...
0
votes
0answers
9 views

Can I load sections like get_template_part for a one pager website?

I'm building a web page with everything on just one page, nothing revolutionary. I'm doing it in a child-theme and have created a new template page for it. I just wonder if it's possible to, instead ...
0
votes
0answers
12 views

Nest HTML content between template part

Apologies for the lackluster title. I am curious if there's something similar in Wordpress as Nunjucks call or block. Essentially, I am curious if it is possible to create a template part and inject ...
1
vote
1answer
132 views

add active class based on permalink and url

i am facing a issue, on my wordpress site in left side i loaded post list with title and excerpt, and when you click on that content will get load on right side of page and its working fine but i ...
1
vote
0answers
14 views

ä/ü/… are not displayed when including via get_template_part()

I just have the problem that characters like ä and ü appear as question marks when I include something via get_template_part(). Does anyone know what this could be? Everywhere else the letters are ...
0
votes
1answer
54 views

What are the benefits of using get_template_part()?

I can find lots of "how" answers in reference to using get_template_part(), but no "why" except that it can cut down the amount of code to be written. I'm asking this from an end user's perspective, ...
0
votes
0answers
30 views

What is better for passing variables to templates - include(locate_template.. or $wp_query->query_vars?

The answers here: Passing variables through locate_template are extremely helpful in solving the problem of passing variables to templates such as sidebar and footer. In short, which is best? ...
2
votes
2answers
3k 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 ...
0
votes
2answers
28 views

Blog template with different header to rest of site

I would like to change one thing on my wordpress custom template - there are big image sliders used throughout the site except on the contact us, checkout and my account pages. The blog page was added ...
0
votes
0answers
85 views

do_action doesn't trigger if it's in a template that gets loaded via REST endpoint

i'm building a single page application using woocommerce and custom REST endpoints to get template parts and load them into the browser ... one endpoint is getting content-single-product.php template ...
1
vote
0answers
108 views

Showing the WordPress standard text area on a Woocommerce Single Product Page?

I'm trying to show the WordPress default text area on a Woocommerce Single Product Page but can't find the appropriate hook to add to the template? I would have thought it would've been well ...
1
vote
3answers
554 views

ACF Relationship + WP Template Parts

Any help with this would be greatly appreciated - I've been grappling with it for days :) I have 2 post types - product + offer I have an ACF relationship field related_products. This field ...
0
votes
2answers
248 views

get_template_part for specific page

I have tried adding a file inside my header.php, but it will not load correctly. I get this error: Fatal error: Uncaught Error: Call to undefined function get_tempalte_part() Here is my markup ...
1
vote
1answer
90 views

Overriding of the template for the Attachment “thumbnails” in the Media Grid isn't working

I'm trying to customize the template of the Media Browser (when clicking on "Add Media" button within Edit Post page), by adding a caption with the filename on each thumbnail so I don't need to select ...
0
votes
1answer
243 views

Shortcode and get_template_part

I'm developing my own plugin for my wordpress. I create a shortcode for my plugin and CPT with a custom taxonomy and custom single-CPT. When I try to use the function get_template_part() in my ...
0
votes
1answer
19 views

Using same variable names in files added with get_template_part()

Say I have a file named content-header.php and content-body.php. In content-header.php I have this variable: $some_var = 'apples'; and in content-body.php I have a variable with the same name but ...
0
votes
0answers
437 views

Display a custom 404 page without a redirect

In a plugin I want to display a specific page without redirecting (and thus without changing the URL) like the plugin https://wordpress.org/plugins/404page/ does. I can capture 404s and redirect them, ...
1
vote
2answers
558 views

add a hook with get_template_part()

I have this hook. How i can add this hook with get_template_part()? I created a file "myfile.php" where I store the HTML code of my search bar, but a can't manage to connect my template add_filter('...
0
votes
1answer
2k views

Echo a shortcode div after every 3 posts

In my wordpress home.php file, I have placed an increment loop with the help of a few stackexchange threads: <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (...
0
votes
0answers
52 views

get_template_part calls amount and performance?

I'm trying to build the theme that is easily customizable by other developers and chose to go get_template_part route so files can be easily overwritten in child theme. My question is there some ...
0
votes
1answer
43 views

Remove a short code and update it's content (AJAX)

This is my code function ajax_layoutcontrol(){ if( !check_ajax_referer( 'special-string','security')){ return wp_send_json_error( 'Invalid nounce'); } if (! current_user_can('...
0
votes
1answer
195 views

How to get value from radio buttons meta box, and return different contents?

I have a multiple checkbox type question (using Ultimate member) storing values as user meta. The question basically asks the user what he wants, and according to what he wants, I would like my ...
1
vote
0answers
47 views

Styles and scripts inside template part

Is there anything wrong with including style and script tags inside a template part? The template part which I'm using like something more similar to a vue/react component is working correctly but ...
2
votes
1answer
231 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
135 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 ...
6
votes
5answers
4k 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() &...
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. $...
0
votes
1answer
760 views

How to reuse get_template_part() containing a simple loop to work with AJAX correctly?

I've got simple loop in standard-loop.php file (which, the file, is located in inc/loop folder): if ( have_posts() ) : while ( have_posts() ) : the_post(); the_content(); ...
0
votes
1answer
397 views

get_template_directory_uri() not working

I am getting trouble with the following lines of code. Actually I have created a custom file confirm_email.php in which I wrote the following piece of code. When I'm removing $temp_dir variable it ...
0
votes
2answers
401 views

How can I show contents of a template part inside of another page?

Wordpress is of course made up of templates and the template parts that make up those templates. I know I can override the template parts - that's not what I'm trying to do here. I'm trying take the ...
1
vote
1answer
77 views

How to show “teaser” posts on blog / archive pages, otherwise full posts

Forgive me, I'm a PHP noob and writing too much of it makes my eyes glaze over; I'm sure this is a super simple solution and probably a dumb question. I'm working on a custom theme where I want to ...
0
votes
1answer
59 views

How to limit the number of Related Posts?

In my theme's file single.php, there is this part of code to get Related Videos: <?php if ( $wp_query ->have_posts() ) { while (have_posts()) : the_post(); ...
0
votes
1answer
813 views

Why would include(locate_template) work but not get_template_part()?

The parent theme uses this construct to include the navbar: include(locate_template( "components/navigation.php" )); I would prefer to use the Wordpress convention: get_template_part( "components/...
0
votes
2answers
130 views

get_template_part causes 500 error

I am trying to list posts in a widget I am working on at the moment. However, when I use the get_template_part function in my loop, my site just keeps loading until I get a 500 http error. $args ...