已将POST更改为GET.现在Ajax正在返回多个帖子


Changed POST to GET. Now Ajax is returning multiple posts

当我使用POST而不是GET进行调用时,等待的时间非常长。因此,由于我实际上没有发送任何数据,我尝试切换到GET。速度提高了一点,但现在它没有抓住我需要的一个帖子,而是返回了几个帖子。我想我需要以某种方式改变函数中的循环,但我不知道该怎么办。有人能帮我吗?

呼叫

$.ajax({
    type: 'get',
    url: ajaxURL,
    data: {'action': 'load-content', post_id: post_id },
    success: function(response) {
        // Wait until all images are loaded
        $('#project-container').html(response).imagesLoaded().then(function() {
            // Fire again to rearrange the slide in the DOM
            resize();
            // Remove all 'hover' classes
            $('article.project').removeClass('hover-sticky grayscale-sticky');
            $('article.project img').removeClass('nohover');
            // Remove the loading icon
            $('.loading-icon').remove();
            // If the user has scrolled...
            if ($(window).scrollTop() !== 0) {
                // First scroll the page to the top
                $('html, body').animate({
                    scrollTop : 0
                },400, function() {
                    matchContainerHeight();
                    projectStyles();
                });
            // If the user has not scrolled...
            } else {
                matchContainerHeight();
                projectStyles();
            }
            return false;
        });
    }
});

功能

<?php
/**
 * Ajax functions
 */
// Return the post content to the AJAX call
function my_load_ajax_content () {
    $args = array(
        'p' => $_POST['post_id'],
        'post_type' => 'projects'
        );
    $post_query = new WP_Query( $args );
    while( $post_query->have_posts() ) : $post_query->the_post(); ?>
    <div class="post-container">
        <div id="project-left-content">
            <?php
            the_title( '<h1 class="entry-title">', '</h1>' );
            the_content();
            if( get_field('client') ): ?>
                <div class="client">
                    Client(s): <?php the_field('client'); ?>
                </div>
            <?php endif; ?>
            <div class="project-cats">
                <?php
                    $cat_names = wp_list_pluck( get_the_category(), 'cat_name');
                    echo join( ', ', $cat_names );
                ?>
            </div>
            <?php if( get_field('url') ): ?>
                <div class="project-link">
                    <a class="first after" href="http://<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a>
                </div>
            <?php endif; ?>
        </div>
        <div id="project-right-content">
            <?php if( have_rows('slides') ): ?>
                <div id="slider">
                    <!-- Slider Setup -->
                    <?php if( have_rows('slides') ):
                        $slideNumber = 0;
                        while ( have_rows('slides') ) : the_row();
                        $slideNumber++;
                    ?>
                        <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">
                    <?php endwhile;endif; ?>
                    <!-- Slide -->
                    <?php if( have_rows('slides') ): ?>
                        <div id="slides">
                            <div id="overflow">
                                <div class="inner">
                                    <?php if( have_rows('slides') ):
                                    while ( have_rows('slides') ) : the_row();
                                        $slideImage = get_sub_field('slide_image');
                                    ?>
                                    <article>
                                        <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
                                    </article>
                                    <?php endwhile;endif; ?>
                                </div><!-- #inner -->
                            </div><!-- #overflow -->
                        </div><!-- #slides -->
                    <?php endif; ?>
                    <!-- Controls -->
                    <?php if( have_rows('slides') ):
                        $slideNumber = 0;
                    ?>
                        <div id="active">
                            <?php while ( have_rows('slides') ) : the_row();
                                $slideNumber++;
                            ?>
                                <label for="slide<?php echo $slideNumber; ?>"></label>
                            <?php endwhile; ?>
                        </div><!-- #active -->
                    <?php endif; ?>
                </div><!-- #slider -->
            <?php endif; ?>
        </div><!-- #project-right-content -->
    </div><!-- .post-container -->
    <?php
    endwhile;
    wp_reset_postdata();
    wp_die();
}
add_action ( 'wp_ajax_nopriv_load-content', 'my_load_ajax_content' ); // when the user is logged in
add_action ( 'wp_ajax_load-content', 'my_load_ajax_content' ); // when the user is not logged in

更改

$args = array(
    'p' => $_POST['post_id'],
    'post_type' => 'projects'
);

$args = array(
    'p' => $_GET['post_id'],
    'post_type' => 'projects'
);

因为您的ajax是GET而不是POST!