簡體   English   中英

wordpress每個類別的帖子數限制

[英]wordpress limit number of posts per category

如何限制Wordpress自定義帖子類型中每個類別的帖子數量?

我正在為擁有我自己的Wordpress主題的客戶制作投資組合網站。 在首頁上,“精選”項目只有8個位置。 創建/更新項目時,可以選中“精選”類別。 如何確定精選的類別不能超過8次。

你可以使用這樣的功能

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
  }          
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM