What is the problem here? Ajax + WP

Hi,

I am trying to load pages with ajax in my wordpress theme but struggling with the php.

This is my current code:

(function($){

   $('body').on('click', 'a', function(e) {

   		e.preventDefault();

        var URL = $(this).attr('href');

        animationFunction();

        var ajaxPromise = $.ajax({
            url: ajaxify.ajaxurl,
            type: "post",
            data: {
		    action: 'my_ajax_submit',
                url: URL
		},
            dataType: "html"
        });

        var animationPromise = animationFunction();
        $.when(ajaxPromise, animationPromise).done(function(data) {
            $('#page-wrap').html(data[0]);
            TweenMax.to('.page', 0.35, { alpha: 1 });
        });

    });

	function animationFunction() {

	  var deferred = $.Deferred();

	  TweenMax.to('.page', 0.35, { alpha: 0, onComplete: deferred.resolve });

	  return deferred.promise();

	}

})(jQuery);

PHP:

function theme_js() {

	wp_enqueue_script( 'ajaxstuff', get_template_directory_uri() . '/js/ajax.js', array('jquery'), '', true );
	wp_localize_script( 'ajaxstuff', 'ajaxify', array(
		'ajaxurl' => admin_url( 'admin-ajax.php' )
	));

}


add_action( 'wp_ajax_nopriv_ajax_submit', 'my_ajax_submit' );
add_action( 'wp_ajax_ajax_submit', 'my_ajax_submit' );

function my_ajax_submit() {

	$pageUrl = $_REQUEST['URL'];
	$postid = url_to_postid( $pageUrl );         
	$post = get_post($postid);
	$pageSlug = get_page_template_slug($postid);

	if ($post) {
	setup_postdata($post);

		get_template_part( $pageSlug );

	}

    exit();
}

As you see I am trying to get whatever template is associated with with the post/page ID. Which well, doesn’t seem to work.

Is it possible to determine if it is a single page or not from post ID? If then I could do something like:

if($post === is_single()){

get_template_part('single');

} else {

get_template_part('page');

}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.