【问题标题】:Adding Pagination to a wordpress post list将分页添加到 wordpress 帖子列表
【发布时间】:2021-04-08 12:31:40
【问题描述】:

我正在尝试将分页添加到此帖子列表https://cutt.ly/tjuH6lJ,我使用的代码是

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<!-- the shortcode to the list -->
...............................
<script> 
var myList = new List('main_blog', {
valueNames: ['pis-li'],
page: 3,
pagination: true
});
</script>

我还尝试了 PHP 分页代码并将其添加到我的 functions.php 中

function pagination_nav() {
global $wp_query;

if ( $wp_query->max_num_pages > 1 ) { ?>
    <nav class="pagination" role="navigation">
        <div class="nav-previous"><?php next_posts_link( '&larr; Older posts' ); ?></div>
        <div class="nav-next"><?php previous_posts_link( 'Newer posts &rarr;' ); ?></div>
    </nav>
<?php }}

并用&lt;?php pagination_nav(); ?&gt;调用它

我正在使用“DIVI”中的“Extra”主题,帖子列表是使用名为“侧边栏中的帖子”的插件制作的

【问题讨论】:

    标签: javascript php wordpress pagination


    【解决方案1】:

    您正在寻找the_posts_pagination() 函数。

    在适用时显示到下一个/上一个帖子集的分页导航。

    它使用get_the_posts_paginationpaginate_links() 函数的参数。

    有关可用参数,请参阅 get_the_posts_pagination()


    分页和循环

    <?php
    if( have_posts() ):
      while( have_posts() ): the_post();
    
        echo '<article>';
        if( get_the_title() !== '' ):
          esc_attr( the_title( '<h1>', '</h1>' ) );
        endif;
        if( get_the_content() !== '' ):
          esc_attr( the_content() );
        endif;
        echo '<article>';
    
      endwhile;
    
      /**
      * the_posts_pagination
      * Displays a paginated navigation to next/previous set of posts, when applicable.
      * @link https://developer.wordpress.org/reference/functions/the_posts_pagination/
      * @link https://developer.wordpress.org/reference/functions/get_the_posts_pagination/
      * @link https://developer.wordpress.org/reference/functions/paginate_links/
      */
      the_posts_pagination( array(
        'screen_reader_text'  => ' ',
        'prev_text'           => '&#x2190;',
        'next_text'           => '&#x2192;',
        'show_all'            => true,
      ) );
    
      else:
    
        echo 'No posts yet!';
    
    endif; ?>
    

    如果我没记错的话,默认情况下,Wordpress 会在显示分页之前显示 12 个帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      相关资源
      最近更新 更多