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

vddsk6oq  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(142)

下面的JS代码解析category.php中的titlecontent,但我无法解析使用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中的titlecontent一起沿着集成到上面的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/

gopyfrb3

gopyfrb31#

在循环中添加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)
);

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

相关问题