How can I get the product category's URL Key?
I can get the category name by $category->getName() but it does not work if I use this $category->getURLKey(),
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
$productCategoryName = $category->getName();
var_dump($category->getURLKey());
}
Return null
Any ideas?
I'm not entirely sure what your goal is, but you're not going to be able to get category information from $_product. Feel free to ask questions based upon my code below. But my code below will grab the active categories children and their info. However, this should be a good enough base for what you're looking to do regardless.
<!-- Use this section if you're trying to grab children categories and their info -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name Feel Free to remove the img src section to remove the image -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- End -->
Any questions feel free to ask! Sorry, I just stole an example from a project so the HTML may not line up for a direct copy.
foreach($categories as $category) {
// for category URL key
var_dump($category->getUrlPath());
// for category URL
var_dump($category->getUrl());
}
Related
Okay, on my blog I have four categories that the user can click to. Management, Industry News, Productivity etc.
Here: http://imgur.com/a/wHqqc
Requirement: I need to find a way using php to link to each category page.
<div class="categories-section">
<div class="category">
<?php
$categories = get_categories();
foreach ($categories as $cat) {
if($cat->cat_name = 'MANAGEMENT') {
$category_link = get_category_link($cat->cat_ID);
}
}
?>
<a href="#"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-1.jpg">
<h3> INDUSTRY NEWS</h3></a>
</div>
<div class="category">
<a href="<?php echo $category_link; ?>"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-2.jpg">
<h3> MANAGEMENT</h3></a>
</div>
<div class="category">
<a href="http://localhost/wordpress/category/PRODUCTIVITY/"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-1.jpg">
<h3> PRODUCTIVITY</h3></a>
</div>
<div class="category">
<a href="http://localhost/wordpress/category/PERSONAL-DEVELOPEMENT/"><img class="category-icon" src="<?php bloginfo('template_url');?>/img/desktop/images/category-icon-2.jpg">
<h3> PERSONAL DEVELOPEMENT</h3></a>
</div>
</div>
Problem: The page css is breaking and it's not working, currently the only way I can to link to category is to hard code it.
Ideas?
You are missing an equal sign (=) in the if-condition in your foreach.
if ($cat->cat_name == 'MANAGEMENT') {
$category_link = get_category_link($cat->cat_ID);
break;
}
You should also break after the result has been found so you don't loop over the other categories.
Update:
I'm not sure if there is a better function in Wordpress to do this, but you could save all links in an associative array to get all links at once.
$wp_categories = get_categories();
$categories = [];
foreach ($wp_categories as $cat)
$categories[$cat->cat_name] = get_category_link($cat->cat_ID);
Now you can do the following:
// Management link:
echo $categories['MANAGEMENT'];
I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.
What I have done so far:
<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
<div class="container">
<div class="col-md-12">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
<?php else: ?>
<strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>| </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif ?>
</div>
I have taken this code from page/html/breadcrumbs.phtml.
I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?
If you want to show category name on category page. You can achieve this by 2 ways.
<?php echo $this->getCurrentCategory()->getName(); ?>
Or
<?php echo Mage::registry('current_category')->getName() ?>
On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends.
At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.
Here's the code, see if it works for you.
//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();
//set CatID to the second element of the array since the first element
//is the base category of which all others are children.
$_catID = $categoryIds[1];
//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);
//get the level of the current category
$level = $_category->getLevel();
//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){
// we want the second level category, since the first is the base category.
// ie if we call the default category 'base' and the product is in category
//base->foo->bar->baz we want to return the link to foo.
// while current category is deeper than 2 we ask it for it's parent
//category until we reach the one we want
while ($level > 2){
$parent = $_category->getParentId();
$_category =Mage::getModel('catalog/category')->load($parent);
$level = $_category->getLevel();
}
//Now we can build the string and echo it out to the page
$caturl = $_category->getUrl_key();
$_linkstring = 'http://www.yourwebsite.com/' . $caturl . '.html';
echo 'in category:';
echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
echo ' '. $_category->getName();
echo '</a>';
}
Get the category name, image, description from category id in magento
$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
Please put the category id in the load function
This is my footer. There is parade of categories with most articles in. I need to exclude here categories with description that starts with "XXX".
So If some categories have description that starts with "XXX", it may donĀ“t show here.
Is it possible please? Im newbie in PHP so I dont know if can I declare category discreption here.
<?php global $teo_options;?>
<footer role="contentinfo">
<?php if(isset($teo_options['enable_popular_companies']) && $teo_options['enable_popular_companies'] == 1) { ?>
<div class="stripe-regular">
<div class="row">
<div class="column">
<h2><?php _e('Name', 'Couponize');?></h2>
</div>
</div>
<div class="row collapse">
<div class="column">
<div class="popular-companies flexslider">
<ul class="rr slides">
<?php
$args['hide_empty'] = 1;
$args['orderby'] = 'count';
$args['order'] = 'desc';
if(isset($teo_options['blog_category']) && $teo_options['blog_category'] != '')
$args['exclude'] = implode(",", $teo_options['blog_category']);
$categories = get_categories($args);
foreach($categories as $category) {
$image = get_option('taxonomy_' . $category->cat_ID);
$image = $image['custom_term_meta'];
?>
<li>
<a href="<?php echo get_category_link( $category->term_id );?>" class="wrapper-5 small">
<img src="<?php echo aq_resize($image, 130, 130, true); ?>" alt="<?php echo $category->name;?> coupons">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
</div>
<?php } ?>
Everything is possible but you're just complicating your problem.
Why would you identify a category by a piece of text in the Description?
Also, searching in the description as it's text could end to be a slow and unnecessary query, if you have a lot of categories.
To solve it, I recommend you take a look at the documentation about Including & Excluding Categories.
What I would do is to make sub-categories and either hide them manually or do a trick between the child and parent categories.
I have successfully created a custom drop-down-menu for each specific category I needed but one of them needs to load the subcategory within a subcategory and I can't get it to work.
The working code without the "subcategory within a subcategory" is the following but I need to find out how to add the 3rd level on this code.
<!-- Vending -->
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = Mage::getModel('catalog/category')->getCategories(355); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getRequestPath(); ?>">
<?php echo $category->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<!-- END - Vending -->
Well, as I have suggested in https://stackoverflow.com/a/14586422/653867 you have to load a category object for your second level categories:
$cat = Mage::getModel('catalog/category')->load($category->getEntityId());
then you can access its children by executing
$children = $cat->getChildrenCategories();
The $children variable is a collection of type Mage_Catalog_Model_Resource_Category_Collection, and you can iterate through it to output the next level categories
I think, your code can be improved a bit if you called getChildrenCategories() on your $main in the first place. You wouldn't have to load every child in a loop, which can be performance punishing. Instead use this (and it can actually be improved even further with recursive calls, but such setup would include creating extra blocks, which might be too much hassle for this particular case):
<?php $main = Mage::getModel('catalog/category')->load(355) ?>
<li class="eight"><?php echo $main->getName(); ?>
<?php $children = $main->getChildrenCategories(); ?>
<ul class="nav_static">
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getUrl(); ?>">
<?php echo $category->getName();
$subCategories = $category->getChildrenCategories();
foreach ($subCategories as $subCat) {
/**
*your code to output the next level categories
*/
}
?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
I'm displaying a list of sub categories by parent category ID and am wanting to display the category image in place of a category name.
Here is what I have so far...
<div id="menu_brands">
<div class="brand_head">
<h3><?php echo $this->__('Browse By Brand') ?></h3>
</div>
<div class="brand_list">
<?php
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
$img = $category->getImageUrl(); //I suspect this line is wrong
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<!--<?php echo $name; ?>-->
<a href="<?php echo $url; ?>" title="<?php echo $name; ?>">
<img src="<?php echo $img; ?>" width="auto" alt="<?php echo $name; ?>" /> <!--I suspect this line is wrong-->
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
I've tried countless ways to display the images in place of the category names but nothing seems to make the images appear. Currently with the above, the output is an empty 'img src' so there is clearly an error with what I'm trying (and probably a better way of achieving what I'm after).
Please could someone kindly point out what the problem is?
If it's of any relevance, what I intend to do afterwards is then display the category images in a grid format (3 or 4 per line).
Many thanks in advance.
The solution by zigojacko is not ideal because it separately loads in models in a loop. This does not scale well, and with many categories will thrash your database. Ideally, you'd add the images to the child collection.
A faster solution is to add the image attribute to a collection with an ID filter like B00MER:
// Gets all sub categories of parent category 'Brands'
$parent = Mage::getModel('catalog/category')->load(6);
// Create category collection for children
$childrenCollection = $parent->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($parent->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);
// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->setOrder('position', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite();
// ADD IMAGE ATTRIBUTE
$childrenCollection->addAttributeToSelect('image');
?>
<ul>
<?php foreach($childrenCollection as $cat): ?>
<li>
<a href="<?php echo $cat->getURL(); ?>" title="<?php echo $cat->getName(); ?>">
<img class="cat-image" src="<?php echo $cat->getImageUrl(); ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
We managed to resolve this ourselves - see fix below.
<?php
//gets all sub categories of parent category 'Brands'
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $data): ?>
<li>
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
Surprised we didn't get more support on this with it being a relatively simple Magento issue. Thanks B00MER for your answer though.
Give this method a try instead of your $img = $category->getImageUrl(); to $img = getCategoryImage($category);
function getCategoryImage($category) {
$categoryCollection = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->getCollection()->addAttributeToSelect('image')->addIdFilter($category->getId());
foreach($categoryCollection as $category) {
return $category->getImageUrl();
}
}
(now defunct) Reference: http://www.magentocommerce.com/boards/viewthread/5026/P45/