Support » Plugins » Hacks » Trouble using load_template() or get_template_part() in plugins

  • Resolved Reuben L. Lillie


    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' ) or query_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 with get_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 usingload_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=”; 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 for load_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=”; 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    I agree that load_template() is the only viable option for plugins, so let’s dispense with any template part issues as not applicable. If you want to include some plugin template fragment, just use the include statement. You can probably load theme template parts using load_template_part(), but not knowing what a theme may have available means it’s not really an option.

    I don’t understand why you are making a custom WP_Query just to alter the ordering. AFAICT, the main query is already mostly setup by WP to do what you want, you can simply alter the main query’s ordering through “pre_get_posts” action. Using the main query is the only reliable way to get pagination to work without taking over the entire process. It’s all or nothing. Partial solutions invariably seem doomed to failure.

    By using the main query, your template loop is going to look different. More like a standard template while: endwhile; loop than what I imagine you have now.

    If you do need to manage pagination yourself, this article explains how to do it. I don’t think you would need to go there, but just in case.

    In summary, use load_template(), use the main query altered through pre_get_posts, and setup your templates with the standard WP loop. Even if this doesn’t entirely work, it’s a good basic approach to use. We can go from here to work out any other issues that may arise.

    Reuben L. Lillie


    Thanks, @bcworkz!

    Both pre_get_posts() instead of WP_Query and include() instead of load_template() seem to be working!

    Refactoring just got *a lot* easier.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trouble using load_template() or get_template_part() in plugins’ is closed to new replies.