有时我们需要让wordpress不同的分类显示不同数量的文章并分页,比如资讯的分类页显示20篇并分页,教程的分类页显示10篇并分页,要如何实现呢?随ytkah一起来看看吧!把下面的代码加入当前主题的function.php文件中

function filter_pre_get_posts( $query ){
  if ( $query->is_main_query() ){
    $num = '';    
    if ( is_category(array(9)) ){ $num = 14; }
    //if ( is_category(array(10)) ){ $num = 14; }
    //if ( is_category(array('questions')) ){ $num = 14; }
    // if ( in_category(array('jhg','hjj','yjj','xxj','jlg')) ){ $num = 10; }
    //if ( is_home() ){ $num = 10; }
    // else if ( is_category() ){ $num = 10; }
    // else if ( is_tag() ){ $num = 10; }
    // else if ( is_date() ){ $num = 10; }
    // else if ( is_author() ){ $num = 10; }
    // else if ( is_search() ){ $num = 10; }
    // else if ( is_archive() ){ $num = 10; }
    if ( '' != $num ){ $query->set( 'posts_per_page', $num ); }
  }
  return $query;
}
add_action('pre_get_posts', 'filter_pre_get_posts');

  修改代码里的分类ID号或者分类目录别名,来控制不同分类不同文章数量分页效果。

  修改代码里的$num = 14 后面的14就是每页显示的文章数量,可以修改成自己需要的数值。

  保存上传。然后在分类页加入分页代码,可以参考这里wordpress数字分页列表导航实现

  参考资料https://www.xuewangzhan.net/wpbbs/17057.html

  另外一种解决方案:不同的分类显示不同的数量文章

  相关文章:wordpress不同分类调用不同的模板

相关文章:

  • 2021-10-16
  • 2021-09-15
  • 2021-07-08
  • 2021-09-08
  • 2021-12-10
  • 2021-12-11
猜你喜欢
  • 2018-10-16
  • 2021-09-08
  • 2021-12-15
  • 2022-01-01
  • 2021-06-13
  • 2021-12-22
  • 2021-11-01
  • 2021-05-10
相关资源
相似解决方案