在Magento中获取类别URL密钥

时间:2014-02-25 06:11:51

标签: php magento url magento-1.8

如何获取Magento中类别的URL密钥。我已在URL密钥字段CMS中添加了此文本:

Category-1

这就是我目前正在尝试在锚点中显示我的类别网址的方式:

$_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; ?>

但每当我检查输出时,它仍然显示如下:

<a href="">
            <span>Manual Tile Cutters</span>
        </a>

我已经检查了谷歌和magento论坛,但我仍然找不到足够的答案。

另外,我试图在锚点中调用URL密钥,还是不同的URL?

6 个答案:

答案 0 :(得分: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; ?>

请注意应用于名为addUrlRewriteToResult()的类别集合的其他方法,并使用getUrl()而不是之前的getCategoryUrl()来调用网址(代码中没有这样的内容) )。

顺便说一下,如果您拨打getUrl(),您的代码应该可以正常运行,但会稍微影响性能。

我希望这会有所帮助。

答案 1 :(得分:16)

也许我完全不明白这个问题,但下面的代码会给你一个给定id的类别的网址

<?php $category = Mage::getModel('catalog/category')->load(4); ?>
<a href="<?php echo $category->getUrl(); ?>">

只需将load()内的id 4改为你需要的那个

答案 2 :(得分:6)

使用Magento(类别)模型可能会因加载类别的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;

阅读this article以获取有关此内容的更多信息。

答案 3 :(得分:3)

使用Mage :: helper('catalog / category')来实现此

<?php $_helper= Mage::helper('catalog/category');
      $_categories = Mage::getModel('catalog/category')->getCollection()
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('is_active'); ?>
<?php foreach($_categories as $_category): ?>
    <a href="<?php echo $_helper->getCategoryUrl($_category); ?>">
          <?php echo $_category->getName(); ?>
    </a>
<?php endforeach;?>

点击hear

的更多信息

答案 4 :(得分:0)

如果要检索商店明智类别属性和类别网址的集合,可以使用

$collection = Mage::getModel('catalog/category')->getCollection()
    ->setStoreId($store->getId())
    ->addAttributeToSelect('entity_id')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('name')
    ->addAttributeToFilter('is_active', 1)
    ->addAttributeToFilter('include_in_menu', 1)
    ->addFieldToFilter('path', array('like'=> "1/{$rootCategoryId}/%"));

    $collection->getSelect()->joinLeft(
    array('url_rewrite_table' => $collection->getTable('core/url_rewrite')),
    "url_rewrite_table.store_id = {$store->getId()} AND id_path = CONCAT('category/',e.entity_id)",
    array('store_url_key' => 'request_path'
    )
    );

检索请求路径,如$ row-&gt; getStoreUrlKey(),并将其作为商店基础Url的前缀。我使用它在管理面板中显示商店特定的类别网格。

答案 5 :(得分:0)

<?php
// View your current directory
echo getcwd();

//create a file handler by opening the file
$myTextFileHandler = @fopen(getcwd()."/../error-20180726.log","r+");

//truncate the file to zero
//or you could have used the write method and written nothing to it
@ftruncate($myTextFileHandler, 0);