Trouble using load_template() or get_template_part() in plugins
-
Background: I’m creating default single, archive, and taxonomy templates for my plugin’s (hierarchical) custom post type(s). I should mention also that I have a custom WP_Query for ordering my post types (i.e.,
'order'=> 'ASC', 'orderby' => array( 'menu_order', 'parent' )
), although, when I test without the custom WP_Query (or even setting'paged' => -1
, to attempt to show all posts) I still experience the same unwanted behavior:Problem: Using
load_template()
for a custom template part (e.g., content-{post-type}.php) only shows one (1) custom post on a given archive-{post-type}.php or taxonomy-{taxonomy}.php page instead of matching the queried pagination parameters.Pagination “works,” insofar as the paged navigation links appear and function, but only one (1) post displays on a given archive/taxonomy page, so page “1” only displays my first post, page “2” only displays my “nth” post (where “n” is equal to
query_var( 'posts_per_archive_page' )
orquery_var( 'posts_per_page')
respectively).Code: As I’ve been developing my plugin’s template files, I’ve been trying to follow WordPress codex recommendations by using
load_template()
instead of working withget_template_part()
(as I would with a theme). However, this snippet appears to be the source of my problem:`if ( $overridden_template = locate_template( ‘some-template.php’ ) ) {
/*
* locate_template() returns path to file.
* if either the child theme or the parent theme have overridden the template.
*/
load_template( $overridden_template );
} else {
/*
* If neither the child nor parent theme have overridden the template,
* we load the template from the ‘templates’ sub-directory of the directory this file is in.
*/
load_template( dirname( __FILE__ ) . ‘/templates/some-template.php’ );
}`(Again, this snippet comes straight from the Codex.)
As a temporary workaround, I’ve been using
get_template_part()
to load my theme’s template parts. However, this introduces an altogether different problem:While all posts appear on all pages as expected, the theme’s ‘content.php’ file is loaded instead of my plugin’s custom ‘content-{post-type}.php’ (which is also expected, given how
get_template_part()
works with themes instead of plugins).At least something “works” (two somethings, by that measure) but I want to be able to package my plugin to display my ‘content-{post-type}.php’ on archive and taxonomy pages as well as single pages.
That is, I have determined, perhaps falsely, that the problem is with my implementation of load_template()
rather than with my particular <strong>'content-{post-type}.php'</strong> file because it works fine when using
load_template()` as the Codex prescribes within my custom <strong>’single-{post-type}.php'</strong> template.I’ve read other places (e.g., on <a href=”https://wordpress.stackexchange.com/questions/94343/get-template-part-from-plugin” target=”_blank”>WordPress Development Stack Exchange</a>, which actually failed to load the template part at all for me) how
get_template_part()
can be assimilated into plugins. However, this seems a bit too far afield, and I would really like to develop this functionality in keeping with WordPress’ recommendations forload_template()
, if at all possible.I have a feeling my problem could have something to do with my implementation of pagination, but, as always, I’m just trying to follow <a href=”https://developer.www.ads-software.com/reference/classes/wp_query/#pagination-parameters” target=”_blank”>the Codex</a> by using:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
.I didn’t think my problem could be so unique, but I haven’t found any other forums or tutorials that cover this sort of (mis)functionality.
Thanks for your assistance.
- The topic ‘Trouble using load_template() or get_template_part() in plugins’ is closed to new replies.