为什么要加载整个页面的HTML,而不是选定的帖子内容?
功能。PHP
function my_action_callback() {
wp_localize_script(\'main_js\', \'ajax_custom\', array(
\'ajaxurl\' => admin_url(\'admin-ajax.php\')
));
}
add_action( \'wp_enqueue_scripts\', \'my_action_callback\' );
function my_php_function_name() {
$the_post_id = $_POST[\'post_id\'];
$args = array(
\'post_type\' => \'offer\',
\'post_status\' => \'publish\',
\'p\' => $the_post_id
);
global $post;
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) { $the_query->the_post();
the_content();
} wp_reset_postdata();
}
}
主要。JS公司
$(".offer .item").click(function () {
var post_id = $(this).attr("data-id")
$("#post-container").html("content loading");
$.ajax({
url: ajax_custom.ajax_url,
type: \'post\',
data: {
action: \'my_php_function_name\',
post_id: post_id
},
success: function (data) {
console.log(data);
jQuery(\'#post-container\').html(data);
}
});
return false;
});