下面的JS代码解析category.php中的
title
和content
,但我无法解析使用PHP代码显示的conference_speaker_business
。
基本上,我也想使用 JQuery JS 代码来解析和显示
conference_speaker_business
。
jQuery(function($) {
$('body').on('click', '.view-post', function() {
var data = {
'action': 'load_post_by_ajax',
'id': $(this).data('id'),
'security': blog.security
};
$.post(blog.ajaxurl, data, function(response) {
response = JSON.parse(response);
$('#postModal h5#postModalLabel').text(response.title);
$('#postModal .modal-body').html(response.content);
var postModal = new bootstrap.Modal(document.getElementById('postModal'));
postModal.show();
});
});
});
这是在 single.php 中显示输出的 PHP 代码,我想将其与category.php 中的
title
和 content
一起集成到上面的 JQuery 解析中
<?php $meta_print_value=get_post_meta(get_the_ID(),'conference_speaker_business',true);
echo($meta_print_value);
?>
我该怎么办?
这是要求的
load_post_by_ajax
:
function load_post_by_ajax_callback() {
check_ajax_referer('view_post', 'security');
$args = array(
'post_type' => 'post',
'p' => $_POST['id'],
);
$posts = new WP_Query( $args );
$arr_response = array();
if ($posts->have_posts()) {
while($posts->have_posts()) {
$posts->the_post();
$arr_response = array(
'title' => get_the_title(),
'content' => get_the_content(),
);
}
wp_reset_postdata();
}
echo json_encode($arr_response);
wp_die();
}
add_action('wp_ajax_load_post_by_ajax', 'load_post_by_ajax_callback');
add_action('wp_ajax_nopriv_load_post_by_ajax', 'load_post_by_ajax_callback');
PS:我正在使用本教程:https://artisansweb.net/load-dynamic-content-on-bootstrap-modal-in-wordpress/
在循环中添加
conference_speaker_business
,如下所示:
$arr_response = array(
'title' => get_the_title(),
'content' => get_the_content(),
'conference_speaker_business'=> get_post_meta(get_the_ID(),'conference_speaker_business',true)
);
现在它将可用于您的
response
变量。以与其他人相同的方式从中获取数据。