分页不适用于自定义帖子类型

时间:2017-05-13 06:43:49

标签: php wordpress pagination

视频说明https://drive.google.com/open?id=0B4HLAyVud6iLNjlHMGF3RXA3Qkk

我正在尝试进行分页,到目前为止它正在显示页面链接,如1 2 3 4>但问题是当我导航到第2页或任何其他页面时,它将我带到404页面。这是我的分页功能

if ( ! function_exists( 'sp_pagination' ) ) { 

    function sp_pagination( $query = '', $echo = true ) {


        $prev_arrow = is_rtl() ? 'fa fa-angle-right' : 'fa fa-angle-left';
        $next_arrow = is_rtl() ? 'fa fa-angle-left' : 'fa fa-angle-right';

        // Get global $query
        if ( ! $query ) {
            global $wp_query;
            $query = $wp_query;
        }

        // Set vars
        $total  = $query->max_num_pages;
        $big    = 999999999;

        // Display pagination
        if ( $total > 1 ) {

            // Get current page
            if ( $current_page = get_query_var( 'paged' ) ) {
                $current_page = $current_page;
            } elseif ( $current_page = get_query_var( 'page' ) ) {
                $current_page = $current_page;
            } else {
                $current_page = 1;
            }

            // Get permalink structure
            if ( get_option( 'permalink_structure' ) ) {
                if ( is_page() ) {
                    $format = 'page/%#%/';
                } else {
                    $format = '/%#%/';
                }
            } else {
                $format = '&paged=%#%';
            }

            $args = apply_filters( 'sp_pagination_args', array(
                'base'      => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
                'format'    => $format,
                'current'   => max( 1, $current_page ),
                'total'     => $total,
                'mid_size'  => 3,
                'type'      => 'list',
                'prev_text' => '<span class="' . $prev_arrow . '"></span>',
                'next_text' => '<span class="' . $next_arrow . '"></span>',
            ) );

            // Output pagination
            if ( $echo ) {
                echo '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
            } else {
                return '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
            }

        }

    }

}

这是我的查询

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $portfolio_grid_query = new WP_Query( 
            array( 
                'post_type'             => 'portfolio',
                'post_status'           => 'publish',
                'name'                  => $portfolio_grid_title_slug,
                'posts_per_page'        => $portfolio_grid_item_limit,
                'category_name'         => $portfolio_grid_cats,
                'order'                 => $portfolio_order,
                'orderby'               => $portfolio_orderby,
                'paged'                 => $paged
            ) 
        );

这里所有的while循环和post结构都在while循环结束之后我把分页函数放到这样:

sp_pagination($portfolio_grid_query, true);

它正确显示所有导航结构,但问题是当我导航它时,我需要404页面而不是页面2来显示其他剩余的帖子。请记住,我总共有9个帖子,我将posts_per_page设置为6,这显示了我当前页面上的6个帖子,如果我点击第2页链接,它应该带我到其他页面显示剩余的3个帖子。

0 个答案:

没有答案