在Magento中创建子CMS页面

时间:2010-08-18 19:45:29

标签: content-management-system magento

我想在Magento中创建一组从属的CMS页面,以便页面顶部的痕迹导航看起来像这样:

  

Home > Parent CMS Page > Child CMS Page

即使我可以指定与URL key字段的层次关系,但默认情况下,Magento中的所有 CMS页面都列在根目录中:

  

Home > Child CMS Page

有什么想法吗?

7 个答案:

答案 0 :(得分:10)

你是对的,Magento中没有CMS页面的层次结构。最好的解决方案是通过向CMS页面添加parent_id并修改CMS模块来处理嵌套URL和面包屑来创建真正的层次结构。但这需要大量编码。

快速而肮脏的黑客是通过在布局更新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 :(得分:1)

几天前我遇到了面包屑和cms页面的同样问题check here ->

类似于Anders Rasmussen的帖子,我手动编辑了每个cms页面

在breadcrumbs.phtml中我添加了一个小条件来定义一个Page是否是一个CMS页面(感谢Alan Storm)并删除标准crumb(s)并通过xmllayout添加新的crumbs。

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

希望有所帮助, RITO

答案 2 :(得分:1)

对于企业(版本1.12),请在以下文件中使用此代码,位置为: 默认 - &GT;模板 - &GT; PAGE-&GT; HTML-&GT; 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; ?>

谢谢,

卡希夫

答案 3 :(得分:1)

这是我解决问题的方法。我对大多数CMS页面使用自定义布局(默认/ template / page / cmsLayout.phtml),所以我将以下代码添加到布局文件中:

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

答案 4 :(得分:1)

<强>更新 -oops-这可能只是一个企业功能


Magento中有一个内置的CMS页面层次结构选项。

要启用它:

系统&gt;配置&gt;一般&gt;内容管理&gt; CMS页面层次结构 - see here

组织层次结构树:

CMS&gt;页面&gt;管理层次结构

要管理页面在树中的位置,请在保存页面后转到其“#34”层次结构选项卡&#34; - 见截图:

enter image description here

在示例页面&#34;我们的文化&#34;是一个&#34;工作的孩子&#34;而不是根目录

答案 5 :(得分:0)

对于Magento Enterprise,我创建了一个扩展来解决这个问题。它使用后端内置的CMS层次结构。该模块名为 Demac / BananaBread ,不需要任何黑客攻击或定制。

直接下载:here

文章链接:http://www.demacmedia.com/magento-commerce/introducing-demac_bananabread-adding-cms-breadcrumbs-from-the-hierarchy-in-magento/

答案 6 :(得分:0)

我构建了一个自定义块来生成碎屑(删除内置并用布局中的自定义替换):

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] : [];
}

然后使用稍微修改过的.phtml模板来显示它们:

<?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; ?>

在2.1

中为我工作正常