更多按钮在WordPress帖子中重复相同的值

时间:2018-08-11 12:07:06

标签: php

`

wordpress查询

<?php    
    $args = array(   
        'category_name'=>'work',
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => '3',
        'paged' => 1,
    );

    $my_posts = new WP_Query( $args );
    if ( $my_posts->have_posts() ) : 

?>

//查询显示帖子              have_posts()):$ my_posts-> the_post()?>             

                          

<?php endif ?>`

javascript

$(document).on('click','.loadmore', function(){
    var page = 2;
    var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
    $.ajax({
        url: ajaxurl,
        type :'post',
        data :{
            page : page,
            action: 'load_posts_by_ajax'
        },
        error :function(response){
            console.log(response);
        },
        success : function(response){
            $('.my-posts').append(response);
            page++;
        }
    });
});

load_posts_by_ajax.php

我得到了重复的价值

<?php      
    add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
    add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');

    function load_posts_by_ajax_callback() {
        //check_ajax_referer('load_more_posts', 'security');
        $paged = $_POST['page'];
        $args = array(
            'category_name'=>'work',
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => '3',
            'paged' => $paged,
        );

        $my_posts = new WP_Query( $args );
        if ( $my_posts->have_posts() ) : while($my_posts->have_posts() ) : $my_posts->the_post() 
?>

        <?php the_title()  the_excerpt() endwhile;  endif; wp_die();

}

0 个答案:

没有答案