I think you should take a look at https://solislab.com/blog/5-tips-for-using-ajax-in-wordpress/ (the old site does not work: http://www.garyc40.com/2010/03 / 5-tips-for-using-ajax-in-wordpress / # admin-ajax ).
By adding a simple function with the right hooks to functions.php , you can get a great way to get exactly what you want.
Adapted to the above address:
add_action( 'wp_ajax_nopriv_myajax-submit', 'myajax_submit' ); add_action( 'wp_ajax_myajax-submit', 'myajax_submit' ); function myajax_submit() { // get the submitted parameters $postID = $_POST['postID']; $response = get_thumbnail_images(); $response = json_encode($response); // response output header( "Content-Type: application/json" ); echo $response; // IMPORTANT: don't forget to "exit" exit; }
I call get_thumnail_images (), where I can have WP_Query or an SQL statement to get the necessary information in an array.
Let's list the wordpress part: 1) hooks
2) that are called based on the action parameter requested by AjaxRequest (see url for the full tutorial)
3) a logical function that will give us thumbnails
4) the result is an array associated with json. You can do whatever you want with it on the front side.
source share