【问题标题】:Hide one of the October Rainlab Blog categories?隐藏十月 Rainlab 博客类别之一?
【发布时间】:2018-04-20 13:04:04
【问题描述】:

如何隐藏 October Rainlab 博客 类别之一? 其中一个类别不应显示在页面上的类别列表中。 我想使用一个隐藏类别仅用于过滤并在主页上显示特殊帖子。 有什么想法吗?

【问题讨论】:

    标签: laravel octobercms octobercms-plugins


    【解决方案1】:

    whereNotIn 雄辩的方法的短一点的版本

    $builder->whereNotIn('id', [22, 32, 44]);

    【讨论】:

      【解决方案2】:

      选择多个 ID 类别。示例。

      use App;
      use October\Rain\Database\Builder;
      
      [...other code ...]
      
      public function boot(){
      
          \RainLab\Blog\Models\Category::extend(function($model) {
              // App::runningInBackend() you can also use this one to make sure it will 
              // execute on frontend only
              if(!App::runningInBackend()) {
                  $model::addGlobalScope('id', function(Builder $builder) {
                      $builder->where([
                          ['id', '!=', 2],
                          ['id', '!=', 3],
                          ['id', '!=', 4]
                      ]);
                  });            
              }
          });
      }
      

      现在不会显示具有 id => 2,3,4

      的类别

      【讨论】:

      • 是的,现在你有了$builder,你可以添加任何你喜欢的条件:)
      【解决方案3】:

      我不确定这里的“隐藏”是什么意思。 但我猜你不想在前端显示它(默认)

      您可以扩展 Category 模型来做到这一点。

      如果你有相关插件 // 创建你自己的插件并在 Plugin.php 文件中定义/覆盖 boot 方法,你可以定义这样的东西

      use App;
      use October\Rain\Database\Builder;
      
      [...other code ...]
      
      public function boot(){
      
          \RainLab\Blog\Models\Category::extend(function($model) {
              // App::runningInBackend() you can also use this one to make sure it will 
              // execute on frontend only
              if(!App::runningInBackend()) {
                  $model::addGlobalScope('id', function(Builder $builder) {
                      $builder->where('id', '!=', 2);
                  });            
              }
          });
      }
      

      现在,在前端它不会显示具有 id => 2

      的类别

      这可能会对您有所帮助,如果您需要其他任何内容,请发表评论。 有关插件相关的详细信息,您可以在此处查看:https://octobercms.com/docs/plugin/registration

      【讨论】:

      • 如何添加多个ID
      • 完成,我找到了解决方案:)
      • 此代码隐藏了类别,但也隐藏了网站所有页面上的帖子。如果有人只想在一个页面上隐藏一个类别,那么只需使用适当的检查。
      猜你喜欢
      • 1970-01-01
      • 2018-11-22
      • 2018-06-17
      • 2019-08-24
      • 2018-11-26
      • 2012-01-31
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多