如何在WordPress中使用JQuery解析查询

vddsk6oq  于 2024-01-06  发布在  WordPress
关注(0)|答案(1)|浏览(185)

下面的JS代码解析category.php中的titlecontent,但我无法解析使用PHP代码显示的conference_speaker_business
基本上,我也想使用JQuery JS代码解析和显示conference_speaker_business

  1. jQuery(function($) {
  2. $('body').on('click', '.view-post', function() {
  3. var data = {
  4. 'action': 'load_post_by_ajax',
  5. 'id': $(this).data('id'),
  6. 'security': blog.security
  7. };
  8. $.post(blog.ajaxurl, data, function(response) {
  9. response = JSON.parse(response);
  10. $('#postModal h5#postModalLabel').text(response.title);
  11. $('#postModal .modal-body').html(response.content);
  12. var postModal = new bootstrap.Modal(document.getElementById('postModal'));
  13. postModal.show();
  14. });
  15. });
  16. });

字符串
这是在single.php中显示输出的PHP代码,我想将其与category.php中的titlecontent一起沿着集成到上面的JQuery解析中

  1. <?php $meta_print_value=get_post_meta(get_the_ID(),'conference_speaker_business',true);
  2. echo($meta_print_value);
  3. ?>


我该怎么做?
以下是load_post_by_ajax的要求:

  1. function load_post_by_ajax_callback() {
  2. check_ajax_referer('view_post', 'security');
  3. $args = array(
  4. 'post_type' => 'post',
  5. 'p' => $_POST['id'],
  6. );
  7. $posts = new WP_Query( $args );
  8. $arr_response = array();
  9. if ($posts->have_posts()) {
  10. while($posts->have_posts()) {
  11. $posts->the_post();
  12. $arr_response = array(
  13. 'title' => get_the_title(),
  14. 'content' => get_the_content(),
  15. );
  16. }
  17. wp_reset_postdata();
  18. }
  19. echo json_encode($arr_response);
  20. wp_die();
  21. }
  22. add_action('wp_ajax_load_post_by_ajax', 'load_post_by_ajax_callback');
  23. 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/

gopyfrb3

gopyfrb31#

在循环中添加conference_speaker_business,如下所示:

  1. $arr_response = array(
  2. 'title' => get_the_title(),
  3. 'content' => get_the_content(),
  4. 'conference_speaker_business'=> get_post_meta(get_the_ID(),'conference_speaker_business',true)
  5. );

字符串
现在它将可用于您的aubricresponse变量。从它获取数据的方式与您为其他变量所做的相同。

相关问题