2

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

You don't need buffer here.

get_template_part('more', 'images');
die();
  • Thank You. However the code isn't working. I receive unexpected string. When I remove line: success: { $( 'main' ).append( data ); it says params is not defined. – Dom.in Feb 9 '18 at 22:49

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.