WordPress CPT编号分页

问题描述

我在WordPress中有一个自定义帖子类型。

移动档案页面是所有帖子的滑块。 桌面存档页面是普通的存档页面,带有编号导航,每页仅显示6个帖子。

我认为这种方法可以正常工作,但是在桌面上应该只有3页,而应该有4页,第四页是空的。

我对自己做错了事感到困惑,因此,如果有人可以看一下,那将是无聊的事。

谢谢

archive-projects.php:

<?php
if ( !have_posts() ) {
    // If no posts match the query
    get_template_part( '404' );
    return;
}

get_header();
?>
    <div class="container">
        <div class="content-area mobile" id="mobile-projects-slider">
            <?php
                $args = array(
                    'post_type'         => 'projects','order-by'          => 'date','order'             => 'des','posts_per_page'    => -1
                );

                $the_query = new WP_Query( $args );
                if($the_query->have_posts() ) :
                    while ( $the_query->have_posts() ) :
                       $the_query->the_post();
            ?>
                <article <?php post_class( 'entry entry-archive' ); ?>  data-aos="fade">
                    <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full' ); ?>
                    <div class="entry-image">
                        <a href="<?php the_permalink(); ?>">
                            <div class="post-image" style="background-image: url('<?php echo $backgroundImg[0]; ?>')"></div>
                        </a>
                    </div>

                    <div class="entry-content">
                        <?php the_title( '<h1 class="entry-title">','</h1>' ); ?>
                        <a href="<?php the_permalink(); ?>" class="more-link">See More</a>
                    </div>
                </article>
            <?php
                    endwhile;
                    wp_reset_postdata();
                else:
                endif;
            ?>
        </div>

        <div class="content-area desktop" id="desktop-projects">
            <?php
                $args = array(
                    'post_type'         => 'projects','paged'             => $paged,'posts_per_page'    => 6
                );

                $the_query = new WP_Query( $args );
                if($the_query->have_posts() ) :
                    while ( $the_query->have_posts() ) :
                       $the_query->the_post();
            ?>
                <article <?php post_class( 'entry entry-archive' ); ?>  data-aos="fade">
                    <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'</h1>' ); ?>
                        <a href="<?php the_permalink(); ?>" class="more-link">See More</a>
                    </div>
                </article>
            <?php
                    endwhile;
                    wp_reset_postdata();
                else:
                endif;
            ?>

            <?php
                get_template_part( '_template-parts/page-navigation' );
            ?>
        </div>

        <!-- <div class="aside"> -->
            <?php /* get_sidebar(); */ ?>
        <!-- </div> -->
    </div>
<?php
get_footer();

page-navigation.php:

<?php if ( is_singular() ) : ?>

    <?php if( get_post_type() == 'projects' ) { ?>
        <div id="navigation" data-aos="fade">
            <div class="pagination pagination-singular">
                <?php if ( get_next_post() ) { ?>
                    <div class="nav-next"><?php next_post_link( '%link','Previous Project' ) ?></div>
                <?php } ?>

                <?php if ( get_previous_post() ) { ?>
                    <div class="nav-previous"><?php previous_post_link( '%link','Next Project' ) ?></div>
                <?php } ?>
            </div>
        </div>
    <?php } else { ?>
        <div id="navigation" data-aos="fade">
            <div class="pagination pagination-singular">
                <?php if ( get_next_post() ) { ?>
                    <div class="nav-next"><?php next_post_link( '%link','Previous Post' ) ?></div>
                <?php } ?>

                <?php if ( get_previous_post() ) { ?>
                    <div class="nav-previous"><?php previous_post_link( '%link','Next Post' ) ?></div>
                <?php } ?>
            </div>
        </div>
    <?php } ?>

<?php else : ?>

    <?php
        aa_numeric_posts_nav();
        // located in includes > functions > theme-functions.php
    ?>

<?php endif; ?>

aa_posts_nav_function():

function aa_numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    // $paged = 0;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }
    //
    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div id="navigation"><ul class="pagination numbered-pagination">' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li class="nav-prev">%s</li>' . "\n",get_previous_posts_link() );

    /** Link to first page,plus ellipses if necessary */
    if ( ! in_array( 1,$links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n",$class,esc_url( get_pagenum_link( 1 ) ),'1' );

        if ( ! in_array( 2,$links ) )
            echo '<li>…</li>';
    }

    /** Link to current page,plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n",esc_url( get_pagenum_link( $link ) ),$link );
    }

    /** Link to last page,plus ellipses if necessary */
    if ( ! in_array( $max,$links ) ) {
        if ( ! in_array( $max - 1,$links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n",esc_url( get_pagenum_link( $max ) ),$max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li class="nav-next">%s</li>' . "\n",get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)