2010-08-18 36 views
8

Tôi muốn tạo ra một bộ cấp dưới của trang CMS trong Magento để điều hướng mẩu bánh mì ở phía trên cùng của trang trông như thế này:Tạo con trang CMS trong Magento

Home > Parent CMS Page > Child CMS Page

Mặc dù tôi có thể chỉ định mối quan hệ phân cấp với trường URL key, có vẻ như là tất cả Trang CMS trong Magento được liệt kê trong thư mục gốc theo mặc định:

Home > Child CMS Page

Bất kỳ ý tưởng nào?

Trả lời

10

Bạn nói đúng, không có phân cấp các trang CMS trong Magento. Giải pháp tốt nhất là tạo một hệ thống phân cấp thực bằng cách thêm parent_id vào các trang CMS và sửa đổi mô-đun CMS để xử lý các URL lồng nhau và đường dẫn. Nhưng điều đó đòi hỏi rất nhiều mã hóa.

Một hack nhanh chóng-và-bẩn là thay đổi vụn bánh mì bằng tay trên mỗi trang con bằng cách thêm một cái gì đó như thế này để cập nhật layout XML:

<reference name="root"> 
<action method="unsetChild"><alias>breadcrumbs</alias></action> 
<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"> 
    <action method="addCrumb"> 
     <crumbName>home</crumbName> 
     <crumbInfo><label>Home page</label><title>Home page</title><link>/</link></crumbInfo> 
    </action> 
    <action method="addCrumb"> 
     <crumbName>myparentpage</crumbName> 
     <crumbInfo><label>My Parent Page</label><title>My Parent Page</title><link>/myparentpage/</link></crumbInfo> 
    </action> 
    <action method="addCrumb"> 
     <crumbName>mysubpage</crumbName> 
     <crumbInfo><label>My Sub Page</label><title>My Sub Page</title></crumbInfo> 
    </action> 
</block> 
</reference> 
+1

Cảm ơn! Đây là chính xác những gì tôi đã làm và, trong khi nó vẫn chỉ là một ghi đè thủ công, nó hoạt động hoàn hảo. – Jason

+0

Chỉ cần sử dụng điều này trên Magento EE 1.12 và nó hoạt động hoàn hảo. –

+0

Tôi không chắc chắn cách thức này sẽ hiệu quả nếu bạn có nhiều cha mẹ và nhiều con. Điều này sẽ không đầu ra chính xác cùng một mẩu tin trên mỗi trang (không tốt)? –

1

tôi đã cùng một vấn đề với vụn bánh mì và cms trang một vài ngày trước check here ->

Tương tự như Anders Rasmussen bài tôi sửa tất cả các cms trang bằng tay

Trong breadcrumbs.phtml tôi đã thêm một điều kiện nhỏ để xác định nếu một trang là một trang CMS (nhờ Alan Storm) và loại bỏ tiêu chuẩn c rumb (s) và thêm mẩu mới qua xmllayout.

<?php 
     if($this->getRequest()->getModuleName() == 'cms'){ 
      unset($crumbs['cms_page']); 
     } 
?> 

xml:

<reference name="breadcrumbs"> 

<action method="addCrumb"> 
    <crumbName>cms_page_1st_child</crumbName> 
    <crumbInfo><label>1st child</label><title>1st child</title><link>/1st child/</link></crumbInfo> 
</action> 

<action method="addCrumb"> 
    <crumbName>cms_page_2nd_child</crumbName> 
    <crumbInfo><label>2nd child</label><title>2nd child</title></crumbInfo> 
</action> 

</reference> 

hy vọng rằng sẽ giúp, Rito

+0

Bạn có phải thêm hành động cho mỗi trang CMS đơn lẻ trong tệp XML này không? –

+0

Có, tôi nghĩ vậy, nhưng vì câu trả lời này khá cũ nên có thể đã lỗi thời. – Rito

1

Đối với doanh nghiệp (phiên bản 1.12) sử dụng mã này ở tập sau, địa điểm là: default-> template-> trang -> html-> breadcrumb.phtml.

<?php 
$cms_id = Mage::getSingleton('cms/page')->getPageId(); 
if($cms_id): 

    if($_SERVER['REQUEST_URI']) { 
     $trim_data = substr($_SERVER['REQUEST_URI'],1); 
     $data = explode('/',$trim_data);   
    } 
    $cms_collection = array(); 
    $url_full = ''; 
    $cms_enterprise = ''; 

    foreach($data as $identifier) { 
     $page_data = Mage::getModel('cms/page')->getCollection() 
         ->addFieldToFilter('identifier', $identifier);   
     if($page_data) { 
      foreach($page_data as $single) { 
       if($single->getContentHeading() != null) {      
        if($single->getPageId()) { 
         $cms_enterprise = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection() 
               ->addFieldToFilter('page_id', $single->getPageId());  
         foreach($cms_enterprise as $single_enterprise) { 
          $url_full = $single_enterprise->getRequestUrl(); 
         }      
        } 
        $cms_collection[] = array($single->getTitle(), $single->getContentHeading(), $url_full); 
       } else {         
        if($single->getPageId()) { 
         $cms_enterprise = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection() 
               ->addFieldToFilter('page_id', $single->getPageId());  
         foreach($cms_enterprise as $single_enterprise) { 
          $url_full = $single_enterprise->getRequestUrl(); 
         }      
        } 
        $cms_collection[] = array($single->getTitle(), $single->getTitle(), $url_full); 
       }     
      }  
     } 
    } 
    $count = count($cms_collection); 
    $i = 1; 
?> 

    <?php if(count($cms_collection)): ?> 
    <div class="breadcrumbs"> 
     <ul> 
      <li> 
       <a href="<?php echo $this->getUrl() /*Home*/ ?>" title="<?php echo $this->__('Home'); /*Home Title*/ ?>"> 
        <?php echo $this->__('Home'); /*Home Name*/ ?> 
       </a>  
       <span>> </span> 
      </li> 
      <?php foreach($cms_collection as $key=>$value): ?>      
       <?php if($i == $count): ?> 
        <li> 
         <strong> 
           <?php echo $value[1]; /*Name*/ ?> 
         </strong> 
        </li>   
       <?php else: ?> 
        <li> 
         <a href="<?php echo $this->getUrl().$value[2] /*Identifier*/ ?>" title="<?php echo $value[0] /*Title*/ ?>"> 
          <?php echo $value[1]; /*Name*/ ?> 
         </a>  
         <span>> </span> 
        </li>   
       <?php endif; ?> 
       <?php $i++; ?> 
      <?php endforeach; ?> 
     </ul> 
    </div> 
    <?php endif; ?> 

<?php else: ?> 
    <?php if($crumbs && is_array($crumbs)): ?> 
    <div class="breadcrumbs"> 
     <ul> 
      <?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?> 
       <li class="<?php echo $_crumbName ?>"> 
       <?php if($_crumbInfo['link']): ?> 
        <a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a> 
       <?php elseif($_crumbInfo['last']): ?> 
        <strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong> 
       <?php else: ?> 
        <?php echo $this->htmlEscape($_crumbInfo['label']) ?> 
       <?php endif; ?> 
       <?php if(!$_crumbInfo['last']): ?> 
        <span>> </span> 
       <?php endif; ?> 
       </li> 
      <?php endforeach; ?> 
     </ul> 
    </div> 
    <?php endif; ?> 

Cảm ơn,

Kashif

0

Đối với Magento Enterprise, tôi đã tạo ra một phần mở rộng để giải quyết này. Nó sử dụng cấu trúc CMS được xây dựng trong phần phụ trợ. Mô-đun này được gọi là Demac/BananaBread và không được yêu cầu bất kỳ lỗi hoặc tùy chỉnh nào.

Download trực tiếp: here

Liên kết đến bài viết: http://www.demacmedia.com/magento-commerce/introducing-demac_bananabread-adding-cms-breadcrumbs-from-the-hierarchy-in-magento/

1

Đây là giải pháp của tôi về vấn đề này. Tôi sử dụng bố cục tùy chỉnh cho hầu hết các trang CMS (mặc định/template/page/cmsLayout.phtml) vì vậy tôi HEVE thêm vào đoạn mã sau vào file layout:

<div class="col-main"> 
    <?php 
     $urlPart=str_replace(Mage::getUrl(),'',Mage::getUrl('', array('_current' => true,'_use_rewrite' => true))); 
     $urlPart=explode('/',$urlPart); 
     $string=''; 
     $return='<div class="breadcrumbs"><ul><li class="home"><a href="'.Mage::getBaseUrl().'" title="Go to Home Page">Home</a><span>/</span></li>'; 
     $count=count($urlPart); 
     foreach($urlPart as $value) 
     { 
      $count--; 
      $string.='/'.$value; 
      $string=trim($string, '/'); 
      $pageTitle = Mage::getModel('cms/page')->load($string, 'identifier')->getTitle(); 
      if($count==0) 
       $return.='<li><strong>'.$pageTitle.'</strong></li>'; 
      else 
       $return.='<li><a href="'.Mage::getBaseUrl().$string.'" title="'.$pageTitle.'">'.$pageTitle.'</a><span>/</span></li>'; 
     } 
    echo $return.'</li></ul></div>'; 
    ?> 
    <?php echo $this->getChildHtml('global_messages') ?> 
    <?php echo $this->getChildHtml('content') ?> 
</div> 
1

cập nhật -oops- này có thể là một tính năng doanh nghiệp chỉ


Có một built-in cây thư mục trang CMS trong Magento.

Để bật tính năng này:

System> Configuration> General> Content Management> CMS Trang Hierarchy - see here

Tổ chức cây phân cấp:

CMS> Các trang> Quản lý Cấu trúc phân cấp

Để quản lý vị trí của trang trong cây, sau khi lưu trang, hãy chuyển đến "thứ bậc tab" - xem ảnh chụp màn hình:

enter image description here

Trong ví dụ trang 'văn hóa của chúng tôi' là một đứa trẻ của 'việc làm' chứ không phải của thư mục gốc

+0

Đây chỉ là trong Magento EE. –

0

tôi đã xây dựng một khối tùy chỉnh để tạo ra các mẩu (loại bỏ các built-in và thay thế với các tùy chỉnh dưới đây trong cách bố trí):

namespace Uprated\Theme\Block\Html; 

class UpratedBreadcrumbs extends \Magento\Framework\View\Element\Template 
{ 
/** 
* Current template name 
* 
* @var string 
*/ 
public $crumbs; 
protected $_template = 'html/breadcrumbs.phtml'; 
protected $_urlInterface; 
protected $_objectManager; 
protected $_repo; 

public function __construct(
    \Magento\Backend\Block\Template\Context $context, 
    \Magento\Framework\UrlInterface $urlInterface, 
    \Magento\Framework\ObjectManagerInterface $objectManager, 
    array $data = []) 
{ 
    $this->_urlInterface = $urlInterface; 
    $this->_objectManager = $objectManager; 
    $this->_repo = $this->_objectManager->get('Magento\Cms\Model\PageRepository'); 
    $this->getCrumbs(); 

    parent::__construct($context, $data); 
} 

public function getCrumbs() { 
    $baseUrl = $this->_urlInterface->getBaseUrl(); 
    $fullUrl = $this->_urlInterface->getCurrentUrl(); 
    $urlPart = explode('/', str_replace($baseUrl, '', trim($fullUrl, '/'))); 

    //Add in the homepage 
    $this->crumbs = [ 
     'home' => [ 
      'link' => $baseUrl, 
      'title' => 'Go to Home Page', 
      'label' => 'Home', 
      'last' => ($baseUrl == $fullUrl) 
     ] 
    ]; 

    $path = ''; 
    $numParts = count($urlPart); 
    $partNum = 1; 
    foreach($urlPart as $value) 
    { 
     //Set the relative path 
     $path = ($path) ? $path . '/' . $value : $value; 

     //Get the page 
     $page = $this->getPageByIdentifier($path); 

     if($page) { 
      $this->crumbs[$value] = [ 
       'link' => ($partNum == $numParts) ? false : $baseUrl . $path, 
       'title' => $page['title'], 
       'label' => $page['title'], 
       'last' => ($partNum == $numParts) 
      ]; 
     } 
     $partNum++; 
    } 
} 

protected function getPageByIdentifier($identifier) { 
    //create the filter 
    $filter = $this->_objectManager->create('Magento\Framework\Api\Filter'); 
    $filter->setData('field','identifier'); 
    $filter->setData('condition_type','eq'); 
    $filter->setData('value',$identifier); 

    //add the filter(s) to a group 
    $filter_group = $this->_objectManager->create('Magento\Framework\Api\Search\FilterGroup'); 
    $filter_group->setData('filters', [$filter]); 

    //add the group(s) to the search criteria object 
    $search_criteria = $this->_objectManager->create('Magento\Framework\Api\SearchCriteriaInterface'); 
    $search_criteria->setFilterGroups([$filter_group]); 

    $pages = $this->_repo->getList($search_criteria); 
    $pages = ($pages) ? $pages->getItems() : false; 

    return ($pages && is_array($pages)) ? $pages[0] : []; 
} 

sau đó sử dụng một mẫu .phtml chút thay đổi để hiển thị chúng:

<?php if ($block->crumbs && is_array($block->crumbs)) : ?> 
<div class="breadcrumbs"> 
    <ul class="items"> 
     <?php foreach ($block->crumbs as $crumbName => $crumbInfo) : ?> 
      <li class="item <?php /* @escapeNotVerified */ echo $crumbName ?>"> 
      <?php if ($crumbInfo['link']) : ?> 
       <a href="<?php /* @escapeNotVerified */ echo $crumbInfo['link'] ?>" title="<?php echo $block->escapeHtml($crumbInfo['title']) ?>"> 
        <?php echo $block->escapeHtml($crumbInfo['label']) ?> 
       </a> 
      <?php elseif ($crumbInfo['last']) : ?> 
       <strong><?php echo $block->escapeHtml($crumbInfo['label']) ?></strong> 
      <?php else: ?> 
       <?php echo $block->escapeHtml($crumbInfo['label']) ?> 
      <?php endif; ?> 
      </li> 
     <?php endforeach; ?> 
    </ul> 
</div> 
<?php endif; ?> 

Làm việc tốt cho tôi trong 2.1

Các vấn đề liên quan