js(编辑4)
$(function() {
$(\'body\').on(\'click\', \'.post-link\', function() {
// Jquery
var ajax = {};
ajax.id= $(this).attr(\'rel\');
var ajaxurl = \'/wp-admin/admin-ajax.php\';
jQuery.post(
ajaxurl,
{
\'action\': \'get_img_post_and_title\',
\'data\': ajax
},
function(response){
// add html
if(response.success){
$(\'#post-cont\').append("<li>ID: "+response.data.post_id+" "+response.data.post_title+" <img src=\'"+response.data.post_thumb+"\'/> --<a href=\'#\' class=\'close\'>X</a></li>");
}
}
);
});
});
php(编辑4)
// php
add_action("wp_ajax_get_img_post_and_title", "get_img_post_and_title");
add_action("wp_ajax_nopriv_get_img_post_and_title", "get_img_post_and_title");
function get_img_post_and_title(){
$return = array(
\'post_id\' => $_POST[\'data\'][\'id\'],
\'post_title\' => get_the_title($_POST[\'data\'][\'id\']),
\'post_thumb\' => wp_get_attachment_image_url(get_post_thumbnail_id($_POST[\'data\'][\'id\']))
);
wp_send_json_success($return);
}
索引。php
<?php
if( have_posts() ):
while( have_posts() ): the_post();
<?php the_post_thumbnail (\'large\', array(\'class\' => \'img-responsive\'));?>
<button class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
endwhile;
endif;
wp_reset_query();
?>
<div id="post-cont"> ...... </div>