php - 如何在自定义页面或模板中为 WordPress 帖子创建分页

标签 php wordpress pagination

我用这个方法

$args = array(
    'posts_per_page' => 4,
    'offset' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true);

$recent_postss = wp_get_recent_posts($args, ARRAY_A);
?> 

foreach ($recent_postss as $posts) { } 

现在我想为此创建一个分页。我已经尝试了很多,但对我没有任何用处。我应该如何修改此代码以创建分页?

最佳答案

试试这个它正在为自定义帖子工作

$args = array(
    'posts_per_page' => 4,
    'offset' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged'    => (get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1),
    'suppress_filters' => true);

用于分页链接

$argsp = array(
  'base'               => '%_%',
  'format'             => '?paged=%#%',
  'total'              => 5,
  'current'            => 2,
  'show_all'           => false,
  'end_size'           => 1,
  'mid_size'           => 2,
  'prev_next'          => true,
  'prev_text'          => __('« Previous'),
  'next_text'          => __('Next »'),
  'type'               => 'plain',
  'add_args'           => false,
  'add_fragment'       => '',
  'before_page_number' => '',
  'after_page_number'  => ''
);


echo paginate_links( $argsp );

关于php - 如何在自定义页面或模板中为 WordPress 帖子创建分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35693805/

相关文章:

php - 清理公众意见的最佳方式是什么?

php - Javascript:使用 javascript 检查多个复选框

javascript - JS : Should I use a switch-case statement or an if/else for this kind of situation?

html - SEO 房地产上市网站 - 索引和链接到 CSS/JS

elasticsearch - 如何在没有偏移的Elasticsearch中对嵌套对象进行分页?

html - Foundation 6 分页,如何对表格进行分页?

php - Laravel 5.2 Eloquent 地将日期时间与当前数据和时间进行比较

php - 检查日期/期间是否在另一个日期/期间内 - 是否可以提供更优雅的解决方案?

mysql - 使 API (Wordpress) 日期可与 Coldfusion 一起使用

language-agnostic - 分页时的偏移量与页码