自定义 post 类型 wordpress 的分页

pagination for custom post type wordpress

我有分页功能 我尝试了很多解决方案但没有任何效果!我使用 WordPress 5.4.2 我的网站是单页网站 当然我想在我的 index.php

中使用它

这是我的代码:

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

$portfolio_post_args = array(
    'post_type'=>'portfolio',
    'posts_per_page' => 2,
    'paged' => $paged,
);
                                $portfolio_post = new WP_Query($portfolio_post_args);

                                 while($portfolio_post->have_posts()) :$portfolio_post->the_post();?>
                    <div id="portfolio1" class="portfolio-layout1 onscroll-animate">
                        <article class="portfolio-item filter-lifestyle">
                            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                    data-animation="fadeInUp">
                                <div class="portfolio-detail">
                                    <div class="portfolio-detail-container">
                                        <div class="portfolio-detail-content">
                                            <h2><?php echo get_the_title( ); ?></h2>
                                            Lifestyle, Other
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>

                        <?php endwhile;

$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' => $portfolio_post_args->max_num_pages
) );
?>

如果这是一个高度重复的问题,我很抱歉 - none 我发现的其他修复对我有用。 请有人告诉我问题出在哪里?

非常感谢!

编辑 1 我尝试了这个,但它也不起作用!

                    <?php
$temp = $wp_query;
$wp_query= null;
$args = array(
    'post_type'   =>   array('portfolio'),
    'paged'       =>$paged,
    'post_per_page' => 6,
     );
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                    <div id="portfolio1" class="portfolio-layout1 onscroll-animate">
                        <article class="portfolio-item filter-lifestyle">
                            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                    data-animation="fadeInUp">
                                <div class="portfolio-detail">
                                    <div class="portfolio-detail-container">
                                        <div class="portfolio-detail-content">
                                            <h2><?php echo get_the_title( ); ?></h2>
                                            Lifestyle, Other
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                        <?php endwhile; ?>
<div class="navigation">
  <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
  <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>
<?php $wp_query = null; $wp_query = $temp;?>

尝试使用此代码。我已对以下代码进行了一些更正。

<?php
$portfolio_post_args = array(
    'post_type'=>'portfolio',
    'posts_per_page' => 2,
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
$portfolio_post = new WP_Query($portfolio_post_args);

while($portfolio_post->have_posts()) : $portfolio_post->the_post();?>
    <div id="portfolio1" class="portfolio-layout1 onscroll-animate">
        <article class="portfolio-item filter-lifestyle">
            <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                    data-animation="fadeInUp">
                <div class="portfolio-detail">
                    <div class="portfolio-detail-container">
                        <div class="portfolio-detail-content">
                            <h2><?php echo get_the_title( ); ?></h2>
                            Lifestyle, Other
                        </div>
                    </div>
                </div>
            </div>
        </article>

<?php endwhile;

$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' => $portfolio_post_args->max_num_pages
) );

wp_reset_postdata();
?>

问题已解决:

            <?php 

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

$data= new WP_Query(array(
    'post_type'=>'portfolio', // your post type name
    'posts_per_page' => 6,
    'paged' => $paged,
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();?>

                    <article class="portfolio-item filter-lifestyle">
                        <div class="popup-window-trigger" data-popup="#<?php echo 'portfolio-pic-'.get_the_ID( ); ?>">
                            <img class="onscroll-animate" alt="img1" src="<?php echo get_the_post_thumbnail_url(); ?>"
                                data-animation="fadeInUp">
                            <div class="portfolio-detail">
                                <div class="portfolio-detail-container">
                                    <div class="portfolio-detail-content">
                                        <h2><?php echo get_the_title( ); ?></h2>
                                        Lifestyle, Other
                                    </div>
                                </div>
                            </div>
                        </div>
                    </article>

            <?php endwhile;?>
            <?php endif;
                 wp_reset_postdata();?>
                </div><!-- #portfolio -->
            <!-- Start Pagination - WP-PageNavi -->
            <br>
            <div id="pagination" class="nav-links">
                <?php        if ( $data->max_num_pages > 1 ) :
                      $big = 999999999;
                      echo '<div class="pagination">';
                      echo paginate_links( array(
                          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                          'format' => '?paged=%#%',
                          'current' => max( 1, get_query_var('paged') ),
                          'total' => $data->max_num_pages,
                          'prev_text' => 'Newer',
                          'next_text' => 'Older'
                      ) );
                      echo '</div>';?>
            </div>
            <!-- End Pagination -->
            <?php endif;?>