【问题标题】:wordpress limit number of posts per categorywordpress 限制每个类别的帖子数
【发布时间】:2017-09-22 12:45:33
【问题描述】:

如何限制 Wordpress 自定义帖子类型中每个类别的帖子数量?

我正在为我自己的 Wordpress 主题的客户制作一个投资组合网站。在主页上,“精选”项目只有 8 个位置。当您创建/更新项目时,您可以检查精选类别。如何确保精选的类别不能被检查超过 8 次。

【问题讨论】:

  • 您是在问如何限制自定义 wp 查询中显示的帖子数?

标签: php wordpress wordpress-theming


【解决方案1】:

你可以使用这样的功能

add_action('pre_get_posts', 'myposts_limit');
function myposts_limit( $query ) { 
  if( !is_admin() && is_tax( 'featured' )) { //change featured with your custom taxonomy
     $query->set( 'posts_per_page', 10); //10 posts on your custom taxonomy
   }        
}

如果您想限制更多,请检查以下功能:

function myposts_limit( $query ) {
 if( $query->is_category() && !is_admin()):
    $query->set('posts_per_page', 14); //14 posts on category
   return;
 endif;
 if( $query->is_search() && !is_admin()):
    $query->set('posts_per_page', 20); //20 posts on search
   return;
 endif; 
 if( $query->is_tag() && !is_admin()):
    $query->set('posts_per_page', 30); //30 posts on tag template
   return;
 endif;  
 if( !is_admin() && is_tax( 'featured' )) { //change featured with your custom taxonomy
      $query->set( 'posts_per_page', 10); // 10 posts on your custom taxonomy
  }          
}

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2015-08-14
    相关资源
    最近更新 更多