mar*_*ike 9 php url magento magento-1.8
如何获取Magento中类别的URL密钥.我已在URL密钥字段CMS中添加了此文本:
Category-1
Run Code Online (Sandbox Code Playgroud)
这就是我目前正在尝试在锚点中显示我的类别URL的方式:
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('is_active');
<?php foreach($_categories as $_category): ?>
<a href="<?php echo $_category->getCategoryUrl($_category); ?>">
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
但每当我检查我的输出时,它仍然显示如下:
<a href="">
<span>Manual Tile Cutters</span>
</a>
Run Code Online (Sandbox Code Playgroud)
我已经检查了谷歌和magento论坛,但我仍然找不到足够的答案.
另外,我正在尝试在锚点中调用URL密钥,还是它是一个不同的URL?
Ron*_*Ron 17
其他两个答案都有DB惩罚.添加类别URL信息的最佳方式是在集合级别,只需在模板文件中使用它.调整代码如下:
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('is_active')
->addUrlRewriteToResult();
<?php foreach($_categories as $_category): ?>
<a href="<?php echo $_category->getUrl($_category); ?>">
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
注意应用于类别集合的附加方法调用addUrlRewriteToResult()
并使用getUrl()
之前getCategoryUrl()
的代码调用url (代码中没有这样的东西).
顺便说一句,如果你打电话,你的代码应该可以正常运行,getUrl()
但会稍微影响性能.
我希望这有帮助.
小智 16
也许我完全不理解这个问题,但下面的代码会给你一个给定id的类别的网址
<?php $category = Mage::getModel('catalog/category')->load(4); ?>
<a href="<?php echo $category->getUrl(); ?>">
Run Code Online (Sandbox Code Playgroud)
只需将load()内的id 4更改为您需要的id
使用Magento(Category-)模型可能会因加载类别的URL而变得非常繁重.当您处于需要加载9000+类别URL的URL的循环中时,您可能会考虑使用url重写功能来获取URL,因为它不涉及加载大量Magento模型:
$requestPath = Mage::getSingleton('core/url_rewrite')
->getResource()
->getRequestPathByIdPath(
'category/' . $categoryId, Mage::app()->getStore()->getId());
return Mage::getBaseUrl() . $requestPath;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61485 次 |
最近记录: |