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";

}

解决方法

解决了。我完全删除了桌面版本的WP_Query(在archive-中,并使用单独的功能添加了它。这就是我现在可以使用的功能:

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
                if(have_posts() ) :
                    while ( have_posts() ) : 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:
                    echo '<h2>No posts found!</h2>';
                endif;
                aa_numeric_posts_nav();
            ?>
        </div>

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

aa_numeric_posts_nav();功能

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";

}

我的桌面上每页CPT帖子的功能:

function aa_order_cpt( $query ) {

    // exit out if it's the admin or it isn't the main query
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // order category archives by title in ascending order
    if ( is_archive() ) {
        $query->set( 'order','desc' );
        $query->set( 'orderby','date');
        $query->set('posts_per_page',6 );
    }
    return;
}
add_action( 'pre_get_posts','aa_order_cpt',1 );