WordPress分页不会在自定义查询

时间:2017-02-17 09:51:01

标签: php wordpress pagination

我创建了一个名为movies的自定义帖子类型,我想在我的索引上显示这个帖子类型,并限制每页的帖子数量为5.索引工作正常但是当我使用echo paginate_links时它会打印页码但页面网址错误。例如,它应该是site.com/?post_type=movies&paged=2但它是site.com/?paged=2

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array('post_type' => array('movies'),
    'paged'=>$paged);
$the_query = new WP_Query( $args );

// loop for all posts ( main page )
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post(); ?>
            <div class="post-wrapper">
                <div class="post-inner-wrapper">
                    <div class="post-title">
                        <span class="title">
                            <?php echo get_the_title() ?>
                        </span>
                    </div>
                    <div class="post-content">
                        <div class="post-outer-text">
                            <span class="post-inner-text">
                                <?php echo get_the_content(''); ?>
                            </span>
                        </div>

                    </div>

                </div>
                <div class="post-image">
                    <?php  the_post_thumbnail(); ?>
                </div>
                <div class="post-details">
                    <span class="calendar">
                        <i class="fa fa-calendar" aria-hidden="true"></i>
                        <span class="calendar_text">
                            <?php the_date(); ?>
                        </span>
                    </span>
                    <span class="author">
                        <i class="fa fa-user" aria-hidden="true"></i><?php the_author(); ?>
                    </span>
                    <span class="view">
                        <i class="fa fa-eye" aria-hidden="true"></i><?php echo get_post_view(get_the_ID()); ?>
                    </span>
                    <span class="like">
                        <i class="fa fa-thumbs-o-up" aria-hidden="true"></i>256
                    </span>
                    <span class="more">
                        <a href="<?php the_permalink()?>">ادامه مطلب</a>
                    </span>
                </div>
            </div>
<?php

    }?>
    <div class="pagination">


    <?php global $wp_query;
    echo ($wp_query->max_num_pages);
    $big = 999999999; // need an unlikely integer
    $page = get_query_var('paged');
    $page = (!empty($page) ? $page : 1);

//            query_posts( 'category_name=category&showposts=2&paged='.$page);
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?p=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );?>

    </div>
<?php
    /* Restore original Post Data */
    wp_reset_postdata();
}
else {
    // no posts found
}

1 个答案:

答案 0 :(得分:0)

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
   'post_type'=>'post',
   'cat' => 6,
   'posts_per_page' => 5,
   'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);

/* PageNavi at Top */
if (function_exists('wp_pagenavi')){wp_pagenavi();}
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();   

/* DO STUFF IN THE LOOP */

endwhile; endif;
/* PageNavi at Bottom */
if (function_exists('wp_pagenavi')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>