在Wordpress中使用AJAX获取特定的页面内容

所以我一直在寻找答案,但找不到答案,但是我已经走了这么远,我只需要一点帮助就能克服困难。

所以我试图从div“ #carslist”中获取特定页面的内容,但是当我进行ajax调用时,它会加载整个页面,而不仅是内容。.我对ajax及其工作原理是相当陌生的。因此任何帮助都将是惊人的!

<div class="cars">
        <script>
            jQuery(document).ready( function() {
        jQuery.ajax({
    type: "GET",url: 'http://staging.idgadvertising.com/carshowcalendars/the-cars-list/',cache: false,dataType: 'html',success: function(data){
               jQuery(".cars").append(data);       
    },error: function(){ },complete: function(){ }
});



            });
            </script>

    </div>

我只需要获取ID为#carslist的div中的内容,但它可以显示整个网页。这是下面我的尝试的屏幕截图

https://imgur.com/1daO18U

biefanwoxingme 回答:在Wordpress中使用AJAX获取特定的页面内容

我没有使用ajax调用页面,而是调用了php文件。我只需要确保它在wordpress循环内,就可以使用短代码。它奏效了,完成了工作,我可以继续我的生活!

<script>
            jQuery(document).ready( function() {
        jQuery.ajax({
    type: "GET",url: '/wp-content/themes/carshowcalendar-child/carslist.php',cache: false,dataType: 'html',success: function(data){
               jQuery("select.ticket-meta").append(data);       
    },error: function(){ },complete: function(){ }
});
            });

</script>

以及该carslist.php文件中的

<?php
define( 'WP_USE_THEMES',false ); // Don't load theme support functionality
require( '../../../wp-load.php' );
echo do_shortcode("[thecars]");

?>

我这样写的简码

/****the cars shortcode*****/

function addcars() {

    $current_user = wp_get_current_user(); 
$userID = $current_user->ID; 
$currentuser_ID = 'user_'.$userID; ?>


    <?php if( have_rows('my_cars',$currentuser_ID) ): ?>
         <?php while( have_rows('my_cars',$currentuser_ID) ): the_row(); ?>


        <option value="<?php echo the_sub_field('year'); ?> <?php echo the_sub_field('make'); ?> <?php echo the_sub_field('model'); ?> <?php echo the_sub_field('color'); ?>"><?php echo the_sub_field('year'); ?> <?php echo the_sub_field('make'); ?> <?php echo the_sub_field('model'); ?> <?php echo the_sub_field('year'); ?> <?php echo the_sub_field('color'); ?></option>
            <?php endwhile; ?>
        <?php endif; ?>

<?php

}
add_shortcode( 'thecars','addcars' );

本文链接:https://www.f2er.com/3149278.html

大家都在问