admin管理员组文章数量:1419230
Why this load whole page HTML, not selected post content?
FUNCTIONS.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();
}
}
MAIN.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;
});
Why this load whole page HTML, not selected post content?
FUNCTIONS.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();
}
}
MAIN.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;
});
Share
Improve this question
asked Jul 23, 2019 at 12:30
Marcin UrbańczykMarcin Urbańczyk
134 bronze badges
2
|
1 Answer
Reset to default 0If all you want is the content of one post:
$post = get_post( $id );
$output = apply_filters( 'the_content', $post->post_content );
ob_clean();
echo $output;
die();
本文标签: Ajax load content from post I clicked
版权声明:本文标题:Ajax: load content from post I clicked 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301136a2652377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
my_php_function_name()
is registered and also whatajax_custom.ajax_url
is set to? – Douglas.Sesar Commented Jul 23, 2019 at 19:33