get_template_part execute with ajax

I would like to get template part with AJAX. This template part – more-images.php contain of source some wp php and html.

I read AJAX is build in WP. So I put code below to my theme files.
My code:

functions.php

function get_img()
    {
    ob_start(); 
    get_template_part('more', 'images');
    die();
    ob_clean();
    }


add_action( 'wp_ajax_get_img', 'get_img' );
add_action( 'wp_ajax_nopriv_get_img', 'get_img' )

javascript file with other functions, loaded in the footer

    $.ajax({
            url: params.url,
            type: 'post',
             data: {  
                action: 'get_img',
success:  { $( 'main' ).append( data );         
     }, });

That code doesn’t work.

1 Answer
1

You don’t need buffer here.

get_template_part('more', 'images');
die();

Leave a Comment