show cms pages in header navigation magento - php

Hi I need in my project to show the CMS pages link dynamically in header,
Can anybody suggest me How I can do this ?

with this code we can show magento cms page dynamically in magento navigation
which have status enabled. We just need to place this code in header or in topnav file
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()->where('is_active = 1'); ?>
<ul id="nav">
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php// print_r($PageData);?>
<?php if($PageData['identifier']!='no-route' && $PageData['identifier']!='enable-cookies' && $PageData['identifier']!='home2') { ?>
<li>
<span><?php echo $PageData['title'] ?></span>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>

Related

Issue in pulling pages from the database

I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>

Magento get CMS pages based on layout

So I'm building a custom breadcrumb nav to list all fundraising options. The pages are using a unique layout called "fundraising_page." Is there a way to grab the pages only if they have the "fundraising_page" layout? So far I have this, which is grabbing every page regardless of the template it is using.
So what I need is only to list the pages that are using the "fundraising_page" template.
<?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>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
Here's some well formatted code and using proper methods.
<?php
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1)
->addFieldToFilter('root_template', 'fundraising_page');
?>
<?php foreach ($collection as $page): ?>
<?php if ($page->getIdentifier() != 'no-route'): ?>
<li>
<?php echo $page->getTitle() ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
Instead of if($PageData['identifier']!='no-route') try
if($PageData['root_template']=='fundraising_page')

Magento How do i add "Home" link accross magento multi store site that shares same theme

I have a magento website with multi store and i have been able to add the home link on the default store. the whole site along with its multi stores shares the same theme but in the theme i edited template/page/navigation/top.php with the code
<?php
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category)
{
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>">
<span><?php echo $this->__('Home') ?></span>
</li>
This code now makes the home link shows but now its only shows in the default store but i want it to show in all other stores, i dont understand this but as they whole stores shares the same theme, i thought maybe they should also be able to read this code and display the home link, i could duplicate the themes and then assign them to each store (which i'm not even sure would work) but that would make the code deficult to maintain as i though if they share the same design, i could just make one change and reflect on the entire sub stores.
The following is the content of template/page/navigation/top.php
<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="nav-container">
<ul id="nav">
<?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
<?php endif; ?>
I hope you are able to help me solve this problem
Use following code
<li class="home <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>"><span><?php echo $this->__('Home') ?></span></li>
I guess the problem is that you put your home link inside of main navigation. As your secondary stores don't have any navigation, the whole unordered list has not been generated.
Try this:
<?php
$_menu = '';
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category){
$_menu .= $this->drawItem($_category);
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<div class="nav-container">
<ul id="nav">
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
Neeraj Garg's answer was the solution in the end, but I wanted to expand on it.
Go to /app/design/frontend/default/yourtheme/template/page/html/topmenu.phtml
That might be specific to my theme. I think most would be using top.phtml in a different folder, as mentioned in the question. Turn on Template Path Hints in the System->Configuration->Advanced->Developer section (you may need to change your scope) to find out what file your navigation menu can be edited in. Make sure you copy and paste it outside of your base theme into your current theme, if it is using the base theme.
After that, I used a similar solution to Neeraj's suggestion. Mine looks like this, and is obviously dependent on the theme I am using (yours will likely look much different, as it will likely need to be catered to your theme).
<li class="level0 nav-0 first level-top <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>">
<a href="\" class="level-top">
<span>Home</span>
</a>
</li>
<?php echo $_menu ?>

Magento - check if cms page

i want to check via php if a page is a cms_page in Magento. I need diffrent breadcrumbs for cms pages, so im trying to this with a condition, but i have no idea how to or where to look at.
Heres my breadcrumbs.phtml so far.
<?php if(this is a cms page): ?>
<p>some content</p>
<?php else: ?>
<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php $charsges = 0; ?>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<?php
$charsges = strlen($_crumbInfo['label']) + $charsges;
if($charsges > 40){
$chars = 18;
if(strlen($_crumbInfo['label']) > $chars){
$_crumbInfo['label'] = substr($_crumbInfo['label'], 0, $chars);
$_crumbInfo['label'] = $_crumbInfo['label'].'..';
}
}
?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?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; ?>
greets rito
The following should give you what you want
//from a block or phtml script
$this->getRequest()->getModuleName()
When this returns the string 'cms', you're on a CMS page.
When Magento's frontend and admin routers can't find a match on your URL, the CMS router takes over. If the CMS router finds a match (based on the CMS pages you've setup), it hands off the request to the cms module and Mage_Cms_IndexController controller.

Magento: Display sub-category list

I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.
I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.
Has anyone had experience of doing this with Magento?
If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat):?>
<?php if($_category->getIsActive()):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
?>
<li>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</li>
<?php endif?>
<?php endforeach;?>
</ul>
If You want to Display top level categories and subcategories U can do Like This..
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
For Displaying Top Level Categories and Current Categories SubCategories you can Do like ....
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This question requires a long answer. I will point you to the right places.
1) Best solution is to use the free extension. I haven't tried it, but it will suit the purpose. You will have to do some CSS to achieve the right look and feel.
http://www.magentocommerce.com/extension/1562/magento-easy-catalog-images
Demo: http://extension01.templates-master.com/gb/electronics.html
2) I do not trust in modules as it might become difficult to upgrade if the vendor decided to stop supporting it. I have used the information from the following forum thread to create a vew sites. Have a look... Might not be straight forward. You might have to make some copies of core files to the local directory.
http://www.magentocommerce.com/boards/viewthread/3770/P30/
Hopefully this will be of help to you :)
I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it’s just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Block
Thanks!
after looking at all the solutions on the magento site, i found that wookiehangover's solution above worked and took about 8 seconds to implement.
creates a UL that you can style. thanks.
After creating static block you can get any list of the subcategories by this script:
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) <= 0) { return; }
$count = 0;
foreach($_subcategories as $_category) {
$category = Mage::getModel('catalog/category')->load($_category->getId());
$ret->{"object_".$count} ->url = $_helper->getCategoryUrl($_category);
$ret->{"object_".$count} ->name = $_category->getName();
$ret->{"object_".$count} ->id = $_category->getId();
$ret->{"object_".$count} ->image = $category->getImageUrl();
$count++;
}
return $ret;
}
$list = list_subcategories(5);
echo "<pre>"; print_r($list); echo "</pre>";
?>
How about listing only the categories belonging to the current item. Not all the categories on the page.
But in a tree like view.
CATEGORIE - sub cat 1
CATEGORIE 2 - sub cat 1 - sub sub cat 1
BR Cveto

Categories