如何使用 JQuery 解析 Wordpress 中的查询

问题描述 投票:0回答:1

下面的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/

javascript jquery ajax wordpress html-parsing
1个回答
0
投票

在循环中添加

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
变量。以与其他人相同的方式从中获取数据。

© www.soinside.com 2019 - 2024. All rights reserved.