Magento static pages menu

2019-02-10 21:26发布

问题:

I want to make a menu that will dynamically show the active static pages from CMS; for example if in my CMS I have these pages:

  • About Us (enabled)
  • Shipping & Refund (disabled)
  • Terms and Conditions (enabled)
  • Contacts (enabled)

then the menu would look like:

About US | Terms and Conditions | Contacts

I need just a few tips on how to get started; maybe somebody has already done this before?

回答1:

Dougle thanks a lot, that was really helpful!

Fede in Magento CMS you can make static pages that you can only access using its IDENTIFIER; what I wanted is somehow make a menu that will automatically display the ACTIVE (enabled) static pages; and if you set status to Disable it should not be in the menu;

here is the code i used, note there is IF $PageData['identifier']!='no-route'; no-rute is the 404 page, so i don't need it in the menu, but it must be enabled so Magento redirects 404 errors to this page;

<div>
    <?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
    <?php  $collection->getSelect()
          ->where('is_active = 1'); ?>
    <ul>
    <?php foreach ($collection as $page): ?>
      <?php $PageData = $page->getData(); ?>
      <?php if($PageData['identifier']!='no-route') { ?>
      <li>
        <a href="/<?php echo $PageData['identifier']?>"><?php echo $PageData['title'] ?></a>
      </li>
      <?php } ?>
    <?php endforeach; ?>
</div>


回答2:

To exclude more than just the no-route I added a new field to the CMS pages to specify if the page should have a menu item or not using true or false. I followed Add a new CMS Field and used the following in main.php

    $fieldset->addField('menu', 'text', array(
        'name'      => 'menu',
        'label'     => Mage::helper('cms')->__('On Menu'),
        'title'     => Mage::helper('cms')->__('On Menu'),
        'required'  => true,
        'disabled'  => $isElementDisabled
    ));

Then changed this line:

<?php if($PageData['identifier']!='no-route') { ?>

to

<?php if($PageData['menu']!= 'false') { ?>


回答3:

Here is another way to put static links to Magento catalog menu.

First, create static page, assign some url key to it, for example, "my-test-page".

Go to /app/code/core/Mage/Catalog/Block, copy file Navigation.php to /app/code/local/Mage/Catalog/Block, now you able to edit it without any worries about the possibility of loosing your changes with Magento upgrade.

Open file Navigation.php at line 265 (magento 1.4) function _renderCategoryMenuItemHtml(...), change code:

    $htmlLi .= '>';
    $html[] = $htmlLi;                    

    $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
    $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
    $html[] = '</a>';

to that:

    $htmlLi .= '>';
    $html[] = $htmlLi;        

    if(preg_match('/\/static-/', $this->getCategoryUrl($category))) {
        $link_url = str_replace("static-", "", $this->getCategoryUrl($category));            
    } else {
        $link_url = $this->getCategoryUrl($category);            
    }

    $html[] = '<a href="'.$link_url.'"'.$linkClass.'>';
    $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
    $html[] = '</a>';

Now go to Categories management, edit category, change URL key to that: "static-my-test-page" and uncheck "Create Permanent Redirect for old URL" check-box. After saving category you will have link to my-test-page at top categories menu in Magento.

So after all that changes you can convert category link to static page link by adding prefix "static-" to category URL key.



回答4:

In your page/html block create a method containing:

$collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());
$collection->getSelect()
    ->where('is_active = 1')
    ->order('main_table.sort_order ASC');
return $collection;

Which you can call in your template and foreach() through creating your LIs

Might need some tweaking mind, depending on your setup.

From memory though i think this is built in, have a look in design/frontend/../../templates/page/ i seem to remember striping out some similar functionality in one of the phtml files in there.

where, order and other select stuff can be found in /lib/Zend/Db/Select.php(FYI)