maybe it is something simple but i am not really good with php
With this code i am able to load the static blocks styled in top of category in Magento and the subcategories in the bottom when the top category has child
What i am trying to do is that when there is no child category it should only load the block
otherwise because of styling it will hold the place that the subcat had position.
Please have a look on my code and maybe you understand:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<div class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></div>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
$_catHtml = $this->getChildHtml('catalog.inline.subcat');
?>
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
// load the blocks styled with ul and il backgrounds
<ul class="inline-categories">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
<?php foreach ($_categories as $_category): ?>
<li<?php if($_i == 1):?> class="first"<?php endif ?>><a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</li>
<?php endforeach ?>
<?php endif; ?>
</ul>
// here i need something like an "else" because the if($count) allready understand that this is not on count so should only load this that is without style //
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId( $this->getCurrentCategory()->getLandingPage() )->toHtml() ?>
//end of this else thing, now should only load the cms/block alone without anything / end of page we are done...
You can use PHP's if/else statements like this:
<?php if ($_count): ?>
stuff if $_count is non-zero
<?php else: ?>
other stuff if $_count is zero
<?php endif; ?>
Related
I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.
Just to give you an idea. I hae 3 levels of categories.
Top Level > Sub Categories > Children of Sub Categories
What I don't want to display is the children of the sub categories.
The below code shows every category. (Sorry the paste didn't format very well.)
<?php
// ---- PRODUCT NAVIGATION ---- \\
$nav_obj = new Mage_Catalog_Block_Navigation();
$store_cats = $nav_obj->getStoreCategories();
$current_cat = $nav_obj->getCurrentCategory();
?>
<ul>
<?php foreach ($store_cats as $cat):?>
<?php
$ids = strtolower(str_replace(" ", "_", $cat->getName()));
$ids = str_replace("&", "", $ids);
?>
<li class="parent" id="<?php echo $ids; ?>"><?php echo $cat->getName()?>
<ul>
<?php foreach (Mage::getModel('catalog/category')->load($cat->getId())->getChildrenCategories() as $childCategory): ?>
<li><a href="<?php echo $childCategory->getUrl(); ?>">
<?php echo $childCategory->getName()?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
I've got a similar thing I'm working on. I'm basically passing over each category and rendering the stuff I want to render:
<?php # get category list ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php foreach($_categories as $_category) : ?>
<?php # turn it into the proper model object ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php echo $_category->getName() ?>
<?php foreach($_category->getChildrenCategories() as $_subCategory) : ?>
<?php echo $_subCategory->getName() ?>
<?php endforeach ?>
<?php endforeach ?>
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.
HI all,
This code works perfectly printing all 5 results with borders on the bottom of each list item through CSS.
However the last item id like there to be no border. How could i add a class to the list item on the last iteration?
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php foreach ($last_articles as $article): ?>
<li>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php endforeach; ?>
</ul>
Thanks for your help.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
BTW, this adds the class "last-item" to the last item processed.
And yes, you should restructure your code so its readable.
You can use the :last-child CSS modifier:
ul.latest-news:last-child { border-bottom: none; }
But it's not so widely supported (yet), so adding an explicit .last class as you're about to do is probably best.
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