Loading partial templates with AJAX/PJAX

I am trying to load content using PJAX (basically AJAX with history). So far I have managed to catch PJAX requests and load the correct content.

My problem is finding the correct template to use for each page. Currently I am using...

$post_type = get_post_type();
locate_template($post_type.'.php', true);

This sort of works but it's not perfect. For example it will never hit front-page.php. Is there a "right" way to do this? What am I missing?

Answers 1

  • If you're concerned about the performance I would recommend using the WordPress API instead of trying to load markup using ajax.

    If you look at wp-includes/template-loader.php you can see how WordPress itself figures out which template to use. You could in theory just load that file, but you might have problems with template_redirect.

    require_once( ABSPATH . WPINC . '/template-loader.php' );
    

    Alternatively, you can just copy paste the code you need.

    $template = false;
    if     ( is_embed()          && $template = get_embed_template()          ) :
    elseif ( is_404()            && $template = get_404_template()            ) :
    elseif ( is_search()         && $template = get_search_template()         ) :
    elseif ( is_front_page()     && $template = get_front_page_template()     ) :
    elseif ( is_home()           && $template = get_home_template()           ) :
    elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
    elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    elseif ( is_attachment()     && $template = get_attachment_template()     ) :
      remove_filter('the_content', 'prepend_attachment');
    elseif ( is_single()         && $template = get_single_template()         ) :
    elseif ( is_page()           && $template = get_page_template()           ) :
    elseif ( is_singular()       && $template = get_singular_template()       ) :
    elseif ( is_category()       && $template = get_category_template()       ) :
    elseif ( is_tag()            && $template = get_tag_template()            ) :
    elseif ( is_author()         && $template = get_author_template()         ) :
    elseif ( is_date()           && $template = get_date_template()           ) :
    elseif ( is_archive()        && $template = get_archive_template()        ) :
    else :
      $template = get_index_template();
    endif;
    /**
     * Filters the path of the current template before including it.
     *
     * @since 3.0.0
     *
     * @param string $template The path of the template to include.
     */
    if ( $template = apply_filters( 'template_include', $template ) ) {
      include( $template );
    }
    

Related Questions