zoukankan      html  css  js  c++  java
  • 如何实现WordPress不同分类则每页显示不同数量的文章

      有时我们需要让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不同分类调用不同的模板

  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/ytkah/p/12149058.html
Copyright © 2011-2022 走看看