0

I'm calling pages with Ajax and I have this code in functions.php to determine which page is called and then, render the corresponding template:

function ajax_callback() {
    if( isset(  $_POST['href'] ) ) {
        $pageId = preg_match( '/^http:\/\/.+\/\?page_id=\d+$/', $_POST['href'] ) ? substr( strrchr( $_POST['href'], '=' ), 1 ) : '8' ;

        if( $pageId === '8' ) get_template_part('home-ajax');
        elseif( $pageId === '22' ) get_template_part('reviews-rvw-ajax');
        elseif( $pageId === '6' ) get_template_part('noticias-news-ajax');
        elseif( $pageId === '109' ) get_template_part('entrevistas-ajax');
    }
}

This works fine, but now I want to call single posts too. I'm getting the post type like this:

$pageId = preg_match( '/^http:\/\/.+\/\?page_id=\d+$/', $_POST['href'] ) ? substr( strrchr( $_POST['href'], '=' ), 1 ) : 'no-page' ;
if( $pageId === 'no-page' && preg_match( '/^http:\/\/.+\/\?news=.+$/', $_POST['href'] ) ) {
        $newSlug = substr( strrchr( $_POST['href'], '=' ), 1 );
} elseif ( $pageId === 'no-page' && preg_match( '/^http:\/\/.+\/\?reviews=.+$/', $_POST['href'] ) ) {
    $reviewSlug = substr( strrchr( $_POST['href'], '=' ), 1 );
}

if( $pageId !== 0 ) {
    if( $pageId === '8' ) get_template_part('home-ajax');
    elseif( $pageId === '22' ) get_template_part('reviews-rvw-ajax');
    elseif( $pageId === '6' ) get_template_part('noticias-news-ajax');
    elseif( $pageId === '109' ) get_template_part('entrevistas-ajax');
} elseif( $newSlug !== '' ) {
            //here I have to get the single post template
            // e.g. $newSlug = slug-post-name, and post_type = 'news'
}

But I don't know how to call this single post template by slug name. I have to call the single.php template without the header and the footer, so I can do:

get_template_part('single-ajax');

//single-ajax.php
<?php
            // Start the Loop.
            while ( have_posts() ) : the_post();

                /*
                 * Include the post format-specific template for the content. If you want to
                 * use this in a child theme, then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );

                // Previous/next post navigation.
                twentyfourteen_post_nav();

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
            endwhile;

But doesn't work, because I should define the query object:

        <?php
 $temp = $wp_query; // assign ordinal query to temp variable for later use  
 $wp_query = null;
 $newSlug = substr( strrchr( $_POST['href'], '=' ), 1 );
 $wp_query = new WP_Query( array( 'name' => $newSlug  ) ); 
 if($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    // Start the Loop.
        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */
        get_template_part( 'content', get_post_format() );

        // Previous/next post navigation.
        twentyfourteen_post_nav();

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }
endwhile;
}
die();

But this is not displaying the post template. Indeed, doesn't begin to execute the loop. Any idea of what is happening or an easier way to achieve this?

1

Well, I've achieved it just adding the post type to arguments on WP Query:

$args = array (
 'post_type' => $post_type,
 'post_status' => 'publish',
 'name'=> $newSlug
);
 $wp_query = new WP_Query( $args ); 

And done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.