【问题标题】:SilverStripe dummy page / menu itemSilverStripe 虚拟页面/菜单项
【发布时间】:2016-07-06 17:47:50
【问题描述】:

想象一下这样的网站结构/菜单布局:

首页
关于我们
服务
__削土豆皮
__切土豆
__烤土豆

所有菜单项都链接到一个真实的页面,带有自己的 URL 和内容。但粗体项目只是一个没有链接、内容或 URL 的菜单项,它的唯一目的是在悬停时折叠子菜单。 SilverStripe 不允许您开箱即用地创建这样的 Page 实体。

我正在寻找一种最简洁、最简单、最简单的方法来创建一个仅用作菜单项的虚拟页面,没有内容,最好也没有 URL slug (后者可能很难)。

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    您无需任何额外代码即可实现“虚拟”页面,只需创建RedirectorPage 并选择您的第一个子页面作为重定向目标。

    就个人而言,我过去使用过更简单的“RedirectorPage”版本,如果直接访问它,它会自动重定向到第一个子页面。

    例子:

    class ChildRedirectorPage extends Page 
    {
        private static $description = 'Page that has no content but automatically redirects to the first of its child-pages';
    
        public function ContentSource() {
            if($firstChild = $this->Children()->First()) {
                return $firstChild;
            } else {
                return $this;
            }       
        }
    
        public function Link($action = null) {
            // prevent link "redirection" when we're in CMS
            if (!is_subclass_of(Controller::curr(),'LeftAndMain')){
                if($firstChild = $this->Children()->First()) return $firstChild->Link($action);
                else return parent::Link($action);
            }
        }
    
        public function getCMSFields() {
            $fields = parent::getCMSFields();
            $fields->removeByName('Content', true);
            return $fields;
        }
    }
    
    class ChildRedirectorPage_Controller extends Page_Controller 
    {
        function init() {
            parent::init();
            if($firstChild = $this->Children()->First()) {
                $this->redirect($firstChild->Link(), 301);
            }           
        }
    }
    

    我认为 URL slug 实际上是有益的,因为您的 URL 将是 services/peeling-potatoes 等,这很可能更适合 SEO。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 2021-07-13
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多