WordPress.org

Ready to get started?Download WordPress

Forums

ajax in wordpress return content-none file (1 post)

  1. orb95
    Member
    Posted 7 months ago #

    In my funcitons.php file i have function that response to ajax call and return posts:
    functions.php:

    function returnPosts(){
        if ( have_posts() ) {
        		while ( have_posts() ) {
        			the_post();
        			get_template_part('content', get_post_format());
        		}
        	} else {
        		get_template_part( 'content', 'none' );
        	}
        exit();
        }
    
        add_action('wp_ajax_returnPosts', 'returnPosts');
        add_action('wp_ajax_nopriv_returnPosts', 'returnPosts');

    Javascript (in index.php):

    jQuery.ajax({
            type:"POST",
            url: "<?php echo admin_url("admin-ajax.php"); ?>",
            data: {"action": "returnPosts"},
            success:function(results){
                // Work with results
            }
        });

    But when i call this function via JS i always get content from content-none.php file.

Reply

You must log in to post.

About this Topic