WordPress教程

WordPress 站点如何限制文章,分类和附件的数量

阿里云

如何限制 WordPress 站点的文章,分类和素材的数量,以商品文章类型为例,讲一下如何限制文章类型的数量:

  1. function wpm_limit_post_type_number($current_screen){
  2.   global $pagenow;
  3.  
  4.   if($pagenow == 'post-new.php'){
  5.     $post_type  = $current_screen->post_type;
  6.  
  7.     if($post_type == 'product'){  // 这里可以改成你需要限制的文章类型
  8.       $counts  = wp_count_posts($post_type);
  9.       $total  = array_sum((array)$counts);
  10.  
  11.       if($total > 500){
  12.         wp_die('商品上限为:500。');
  13.       }
  14.     }
  15.   }
  16. }
  17.  
  18. add_action('current_screen', 'wpm_limit_post_type_number');
也想出现在这里?联系我们
创客主机

使用上面这段代码之后,再点击新增商品,就会出现:

以商品分类这个分类模式为例,讲一下如何限制分类模式的数量:

  1. function wpm_limit_taxonomy_number($term, $taxonomy){
  2.   if($taxonomy == 'product_category'){  // 这里可以改成你需要限制的分类模式
  3.     if(wp_count_terms($taxonomy) > 10){
  4.       return new WP_Error('too_much_product_category', '商品分类上限为:10。');
  5.     }
  6.   }
  7.  
  8.   return $term;
  9. }
  10.  
  11. add_filter('pre_insert_term', 'wpm_limit_taxonomy_number', 10, 2);

使用上面这段代码之后,再新增商品分类的时候,就会出现:

媒体素材附件是最占资源的,这个运营 SaaS 就不得不限制了:

  1. function wpm_limit_attachement_count($file){
  2.   $counts  = wp_count_posts('attachment');
  3.   $total  = array_sum((array)$counts);
  4.  
  5.   if($total > wpm_ShopCountLimit::get_limit('attachment')){
  6.     $error  = wpm_ShopCountLimit::get_error('attachment');
  7.     $file['error']  = $error->get_error_message();
  8.   }
  9.  
  10.   return $file;
  11. }
  12.  
  13. add_filter('wp_handle_upload_prefilter', 'wpm_limit_attachement_count');

使用上面这段代码之后,在后台再上传图片的时候,就会出现:

当然运营 SaaS 还有其他地方和做一个单独博客是不一样的,今天主要就是对资源限制最一些粗浅的介绍,你对 SaaS 平台技术和运营有什么看法,可以一起来探讨。

WordPress 站点如何限制文章,分类和附件的数量

已有 123 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!