【问题标题】:pagination for custom post type wordpress自定义帖子类型 wordpress 的分页
【发布时间】:2020-07-29 15:04:42
【问题描述】:

我用分页存货我尝试了很多解决方案但没有任何效果!我使用 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
) );
?>

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

非常感谢!

编辑 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;?>

【问题讨论】:

    标签: wordpress pagination custom-post-type


    【解决方案1】:

    试试这个代码。我对下面的代码做了一些更正。

    <?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();
    ?>
    

    【讨论】:

    • 谢谢你的回答,但它说出乎意料);删除括号后它不起作用。
    • @rezarezakhani 您使用的是什么永久链接?你在使用普通的永久链接吗?
    • 我现在明白了,是的,我使用普通的永久链接
    【解决方案2】:

    问题解决了:

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

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 1970-01-01
      • 2016-08-08
      • 2017-10-14
      • 2012-11-29
      • 2017-06-04
      • 2013-06-22
      相关资源
      最近更新 更多