使用wp_query加载ajax时如何分页?

时间:2014-12-17 06:46:22

标签: ajax wordpress

我有一个运行PHP WP_Query函数的AJAX函数,我不确定如何使用分页动态输出这些结果。 function.php中的代码

function feature_procject_list() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ): ?>
  <?php 
        $post_type = $_REQUEST['post_type'];
        $id_cat = $_REQUEST['id_cat'];
        $paged  = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
        $arr_2  = array(
        'posts_per_page'=> 4,
        'cat'     => $id_cat,
        'paged' => $paged,
        ); 
        $wp_query = new WP_Query( $arr_2 );
        ?>
        <?php if($wp_query->have_posts()): ?>
          <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            <div class="single_project">
            <a class='img_thumb' href="<?php echo get_permalink();?>"><?php the_post_thumbnail('medium'); ?></a>
            <h4><a href="<?php echo get_permalink();?>"><?php the_title(); ?></a></h4>
             <p class="date"><?php the_date('j F \a\t G:i'); ?></p>
            <?php the_excerpt(); ?>
            </div>
          <?php endwhile; 
          //wp_reset_postdata();
          ?>
          <div id="paging_wrap">
            <?php my_pagination(); ?>
            <?php //paginate(); ?>
          </div><!-- #paging_wrap -->
        <?php else: ?>
          <h4><?php _e( 'Sorry, no posts matched your criteria.' ); ?></h4>
        <?php endif; ?>
<?php endif;die();
}
add_action( 'wp_ajax_feature_procject_list', 'feature_procject_list' );
add_action( 'wp_ajax_nopriv_feature_procject_list', 'feature_procject_list' );

function my_pagination() {
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
}

并在页面模板上获取它:

function get_list_feature_project(id_category) {
        $.ajax({
            url: ajaxurl,
            data: {
                'action':'feature_procject_list',
                'id_cat' : id_category
            },
             beforeSend : function(){
                    $('#loading').show();
            },
            success:function(data) {
                // This outputs the result of the ajax request
                $('#loading').hide();
                $('.list_post').html(data);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        }); 
    }

帖子仍然显示,但传呼不起作用。

0 个答案:

没有答案