【问题标题】:How to implement wordpress pagination如何实现wordpress分页
【发布时间】:2023-03-11 05:56:01
【问题描述】:

我已经找了分页脚本,把它们放在下面的代码中,但是没有用,我如何在下面实现分页?

'<?php
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
  setup_postdata( $post ); ?>

  <div id="pbox">

    <div id="pthumb"><a href="<?php the_permalink(); ?>" class="ptitle"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></a></div>
        <div id="pcontent">
    <a href="<?php the_permalink(); ?>" class="ptitle"><?php the_title(); ?></a>
    <?php the_excerpt(); ?><br />

Post Category: <b><?php the_category( ', ' ); ?></b>



        </div>
    </div>

<?php endforeach; 
wp_reset_postdata(); ?>'

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    试试这个:

    你需要在argument中传递paged参数。然后在函数paginate_links中传递max_num_pages

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'posts_per_page' => 10 ,'paged'=>$paged);
    $the_query = new WP_Query( $args ); 
    while ($the_query ->have_posts()) : $the_query -> the_post();
     ?>
    <div id="pbox">
    
        <div id="pthumb"><a href="<?php the_permalink(); ?>" class="ptitle"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></a></div>
            <div id="pcontent">
        <a href="<?php the_permalink(); ?>" class="ptitle"><?php the_title(); ?></a>
        <?php the_excerpt(); ?><br />
    
    Post Category: <b><?php the_category( ', ' ); ?></b>
    
    
    
            </div>
        </div>
    <?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' => $the_query->max_num_pages
    ) );
    wp_reset_query();
    

    【讨论】:

    • 好的,它可以显示分页链接,但是当我点击任何数字示例 2 或 3 时,它只显示帖子的主页列表,为什么?
    • 点击页码后,是重定向到同一个页面还是另一个页面? @TimSharpe
    • 另一个页面,上面有相同的帖子
    • 好的。请从后端(wp-admin)设置默认永久链接,然后再次检查。! @TimSharpe
    • 我已尝试将永久链接更改为所有选项,但仍然无法更正
    猜你喜欢
    • 2017-07-04
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2012-06-03
    相关资源
    最近更新 更多