Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Show CMS Pages on Top in Dynamic Menus with sort order
 
ahmadTheGeek
Jr. Member
 
Total Posts:  3
Joined:  2008-07-17
 

How to show CMS pages as menu

app/design/frontend/default/default/template/catalog/navigation
or whatever is your store front skin folder

in
top.phtml

after:

<?php foreach ($this->getStoreCategories() as $_category): ?>
            <?php 
echo $this->drawItem($_category?>
        <?php 
endforeach ?>
Add:

<?
    $store 
Mage::app()->getStore('default')->getCode();
    
    
$cms_pages Mage::getModel('cms/page')->getCollection();
    
$cms_pages->addStoreFilter($store);
    
$cms_pages->load();

        echo 
'<li onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)" class="level0 parent">'."\n";
        echo 
'<a href="home"><span>Information/Home</span></a>'."\n";
        echo 
'<ul class="level0">'."\n";
        
$last count($cms_pages);
        
$lastText '';
        
$ctr 0;
        foreach(
$cms_pages as $_page)
        
{
            $data 
$_page->getData();
            if(
$data['identifier']=='no-route')
                continue;

            if(
$ctr++ == $last)
                
$lastText ' last';
            echo 
'<li class="level1'.$lastText.'" ><a href="'.$data['identifier'].'"><span>'.$data['title'].'</span></a></li>'."\n";
        
}
        
echo '</ul></li>';
        
?>
To enable sort order on the frontend for menu
This is optional if you do not want to make changes to the core then do not make the following changes

/app/code/core/Mage/Cms/Model/Mysql4/Page
line 95
Replace:

->orWhere('store_table.store_id=?'$store);

with:

->orWhere('store_table.store_id=?'$store)
        ->
order('sort_order');

To enable Sort Order Editing in Admin

app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit/Tab

in

Main.php

after:

$fieldset->addField('identifier''text', array(
            
'name'      => 'identifier',
            
'label'     => Mage::helper('cms')->__('SEF URL Identifier'),
            
'title'     => Mage::helper('cms')->__('SEF URL Identifier'),
            
'required'  => true,
            
'class'     => 'validate-identifier',
            
'after_element_html' => '<span class="hint">' Mage::helper('cms')->__('(eg: domain.com/identifier)') . '</span>',
        ));
Add:

$fieldset->addField('sort_order''text', array(
            
'name'      => 'sort_order',
            
'label'     => Mage::helper('cms')->__('Sort Order'),
            
'title'     => Mage::helper('cms')->__('Sort Order'),
            
'required'  => false,
        ));


To view the Sort order for CMS pages in listing in Admin

app/code/core/Mage/Adminhtml/Block/Cms/Page

in

Grid.php

after:

$this->addColumn('is_active', array(
            
'header'    => Mage::helper('cms')->__('Status'),
            
'index'     => 'is_active',
            
'type'      => 'options',
            
'options'   => array(
                
=> Mage::helper('cms')->__('Disabled'),
                
=> Mage::helper('cms')->__('Enabled')
            ),
        ));

Add

$this->addColumn('sort_order', array(
            
'header'    => Mage::helper('cms')->__('Sort Order'),
            
'index'     => 'sort_order',
            
'type'      => 'text',
        ));

Thats it. I hope it helps some of you as I had really wasted a lot of time for this small thing.

 
Magento Community Magento Community
Magento Community
Magento Community
 
leetlamer
Jr. Member
 
Total Posts:  1
Joined:  2008-07-25
 

Thanks, this was helpful.

For some reason when I the code (the first part only) no back-end modifications, it will only show the About page, none of my other CMS pages.

I can understand why you wouldn’t want to show your 404 pages and such, but none of my other pages besides About Us will show up. I don’t really have a firm grasp of PHP at the moment so I haven’t been able to figure out how your script works.

 
Magento Community Magento Community
Magento Community
Magento Community
 
violetweb
Jr. Member
 
Total Posts:  26
Joined:  2008-07-11
 

Your posted solution only works with the ‘default’ store. 
Even when I edit the getStore(’goblinweb’) to point to a different website, it still picks up the default (global) pages.
Do you have a solution for this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jeremy Timms
Jr. Member
 
Total Posts:  25
Joined:  2008-04-01
 

This as the other poster states, does not work on anything below ‘about us’ - no other new or other cms pages are showing :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
augirl
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-07-21
 

Hi Jeremy,

If I understand you correctly, what you need to do to get the other CMS pages to list is to add the code posted by ahmadTheGeek (in app/design/frontend/default/default/template/catalog/navigation/top.phtml)

<?
    $store 
Mage::app()->getStore('default')->getCode();
    
    
$cms_pages Mage::getModel('cms/page')->getCollection();
    
$cms_pages->addStoreFilter($store);
    
$cms_pages->load();

        echo 
'<a href="http://yoursitename.com.au/home"><span>Home</span></a>'."\n";
        echo 
'<ul class="level0">'."\n";
        
$last count($cms_pages);
        
$lastText '';
        
$ctr 0;
        foreach(
$cms_pages as $_page)
        
{
            $data 
$_page->getData();
            if(
$data['identifier']=='no-route')
                continue;

            if(
$ctr++ == $last)
                
$lastText ' last';
            echo 
'<li class="level1'.$lastText.'" ><a href="'.$data['identifier'].'"><span>'.$data['title'].'</span></a></li>'."\n";
        
}
        
echo '</ul></li>';
        
?>

for each page you want listed.  (Note, I’ve included a full url in the code above as that seems to allow for subdirectories etc.)

This unfortunately means that each time you add a new page you need to update the top.phtml file.

I hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
naujasdizainas
Sr. Member
 
Avatar
Total Posts:  128
Joined:  2007-11-04
Lithuania, Vilnius
 

This code work with change lang

<div class="navigacija_topas">
    
<?
    
//CMS puslapiu iskyrimas automatiskai
    
$store Mage::app()->getStore();

   
$cms_pages Mage::getModel('cms/page')->getCollection();
    
$cms_pages->addStoreFilter($store);
    
$cms_pages->load();

        echo 
'<ul id="main-nav">'."\n";
        
$last count($cms_pages);
        
$lastText '';
        
$ctr 0;
        foreach(
$cms_pages as $_page)
        
{
            $data 
$_page->getData();
            if(
$data['identifier']=='no-route')
                continue;

            if(
$ctr++ == $last)
                
$lastText ' last';
            echo 
'<li class="level1'.$lastText.'" ><a href="'.Mage::getURL('').''.$data['identifier'].'" title="'.$data['title'].'" target="_self"><span>'.$data['title'].'</span></a></li>'."\n";
        
}
        
echo '</ul></li>';
        
?> </div>

Replaced this in code

$store Mage::app()->getStore('default')->getCode();
TO
$store Mage::app()->getStore();

And add dinamic url <a href=“‘.Mage::getURL(’’).’’.......

 
Magento Community Magento Community
Magento Community
Magento Community
 
M. Qasim Ali
Jr. Member
 
Total Posts:  4
Joined:  2008-09-22
Islamabad, Pakistan
 

Ok…

The above code actually list down all the CMS pages in a drop down menu. But is there any way we can list the CMS pages horizontally on the top menu just like the way Categories displyed?

 
Magento Community Magento Community
Magento Community
Magento Community
 
naujasdizainas
Sr. Member
 
Avatar
Total Posts:  128
Joined:  2007-11-04
Lithuania, Vilnius
 

Mke in CSS change....

 
Magento Community Magento Community
Magento Community
Magento Community
 
Creative_Glance
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2008-09-09
 

thanks for the help but how can i give “active” class to CMS page list

which condition will used to identify current open page as active page in this.

many thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Creative_Glance
Jr. Member
 
Avatar
Total Posts:  20
Joined:  2008-09-09
 

hi

i got the solution to add “active” class to active CMS page

need to change the code like

<?
    $store 
Mage::app()->getStore('default')->getCode();
    
    
$cms_pages Mage::getModel('cms/page')->getCollection();
    
$cms_pages->addStoreFilter($store);
    
$cms_pages->load();

        
$last count($cms_pages);
        
$lastText '';
        
$ctr 0;
        foreach(
$cms_pages as $_page)
        
{
            $data 
$_page->getData();
            if(
$data['identifier']=='no-route' || $data['identifier']=='home')
                continue;
            
            if(
Mage::helper('core/url')->getCurrentUrl()== Mage::getURL('').''.$data['identifier']{
                $str_active_class    
=    "active";
            
else {
                $str_active_class    
=    "";
            
}
            
            
if($ctr++ == $last)
                
$lastText ' last';
            echo 
'<li class="level1'.$lastText.' '.$str_active_class.' " ><a href="'.Mage::getURL('').''.$data['identifier'].'"><span>'.$data['title'].'</span></a></li>'."\n";
        
}
        ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
litmusdesigns
Jr. Member
 
Total Posts:  11
Joined:  2008-07-11
 

I modified this code to create a menu of “URL aware” CMS page links. Links of CMS pages which are in the same path as the current page are shown, others are ignored. See posting here http://www.magentocommerce.com/boards/viewthread/24881/#t82643. I welcome any feedback.

 
Magento Community Magento Community
Magento Community
Magento Community
 
unfeasible
Sr. Member
 
Avatar
Total Posts:  94
Joined:  2008-03-03
New Zealand
 

Dear Augirl,

WOAH. you answer is perfect for listing the cms pages in a sidebar block. Thank you so much!

 
Magento Community Magento Community
Magento Community
Magento Community
 
swbratcher
Member
 
Avatar
Total Posts:  43
Joined:  2008-07-21
 

The code on this page doesn’t work perfectly if there are no-routes found causing the skipping of the $cnt increment. I’ve improved it with the below code. I’ve included the “first”, “last” and “active” class injections. This makes sure “first” and “last” classes work if there are no-routes except in the situation where the last one is a no-route where the “last” class will still not be inserted.

NOTE: I also ditched the dropdown functionality as I’m using this as a horizontal navigation in my header that has no dropdowns. If I modify this along those lines I’ll post my updates here. Enjoy.

<? ## Added by SwB for CMS menu display... Worked as of Magento v.1.3.2.1 ?>
<div class="header-nav-container">
    <
div class="header-nav">
        
<?
        $store 
Mage::app()->getStore('default')->getCode();
        
        
$cms_pages Mage::getModel('cms/page')->getCollection();
        
$cms_pages->addStoreFilter($store);
        
$cms_pages->load();

        echo 
'<ul id="cms-nav">'."\n";
        
$first 0;
        
$last count($cms_pages);
        
$ctr 0;
        foreach(
$cms_pages as $_page)
        
{
            $data 
$_page->getData();
            
$stepText '';
            if ( 
$ctr == $last  $stepText ' last';
            if ( 
$ctr == $first $stepText ' first';
            
$activeText = (Mage::helper('core/url')->getCurrentUrl()== Mage::getURL('').''.$data['identifier'])?" active":"";
            
$ctr++;
            if(
$data['identifier']=='no-route'){
                $first
++;
                
$last--;
                continue;
            
}
            
echo '<li class="level0'.$stepText.$activeText.'" ><a href="'.$this->getUrl($data['identifier']).'"><span>'.$data['title'].'</span></a></li>'."\n";
        
}
        
echo '</ul>';
        
?> 
    
</div>
</
div>
<? ## End SwB addition. ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Compuland
Guru
 
Total Posts:  374
Joined:  2009-01-14
Denmark
 

What exactly is it this extension does?

 
Magento Community Magento Community
Magento Community
Magento Community
 
KjoeLjan
Jr. Member
 
Total Posts:  5
Joined:  2010-02-26
 

I know I am bumping a pretty old thread, but I just have to comment on this.

Although this piece of code might do exactly what you want and is what you are looking for, please always remember that
Logic in templates is bad practice!
No matter what.

KL.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tiagomatos
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-03-26
 

->orWhere(’store_table.store_id=?’, $store);

is a bit outdated.

at Magento 1.4 /app/code/core/Mage/Cms/Model/Mysql4/Page/Collection.php@101 paste this code

$this->getSelect()->join(
array(’store_table’ => $this->getTable(’cms/page_store’)),
‘main_table.page_id = store_table.page_id’,
array()
)
->where(’store_table.store_id in (?)’, ($withAdmin ? array(0, $store) : $store))
->group(’main_table.page_id’)->order(’sort_order’);

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2