【问题标题】:Get category URL key in Magento在 Magento 中获取类别 URL 键
【发布时间】:2014-03-27 04:40:53
【问题描述】:

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

Category-1

这就是我目前尝试在锚点中显示我的类别 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; ?>

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

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

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

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

【问题讨论】:

  • 终于解决了你的url问题。

标签: php magento url magento-1.8


【解决方案1】:

如果您想检索存储明智的类别属性和类别 url 的集合,您可以使用

$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->getStoreUrlKey() 并在其前面加上商店基础 Url。我正在使用它在管理面板中显示商店特定的类别网格。

【讨论】:

    【解决方案2】:

    为此使用 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

    【讨论】:

      【解决方案3】:

      使用 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;
      

      阅读this article了解更多信息。

      【讨论】:

        【解决方案4】:

        也许我没有完全理解这个问题,但下面的代码会给你一个给定 id 的类别的 url

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

        只需将 load() 中的 id 4 更改为您需要的那个

        【讨论】:

          【解决方案5】:

          其他两个答案都有数据库惩罚。添加类别 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() 调用url,而不是之前的getCategoryUrl()(代码中没有这样的东西)。

          顺便说一句,如果您调用getUrl(),您的代码应该可以正常工作,但会稍微影响性能。

          我希望这会有所帮助。

          【讨论】:

          • 不幸的是,每个 getUrl($_category) 调用仍然会导致额外的数据库查询,所以我不同意这是非常有效的。
          • 如果提前在类别集合中预取,则不会。 getUrl() 将从集合对象中获取数据,并且不需要额外的数据库查询。试试看。
          • 是的,我试过了。我的 mysql 日志显示每个 getUrl() 调用都进行了额外的查询
          • 在 Magento 1.9.3.10 中 $category->getUrl() 是否仍然指向完整的 URL 而不是域 .... $this->getCategoryUrl($category) 也是如此。 ...并且 $category->getUrlKey() 返回 NULL
          猜你喜欢
          • 2014-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-06
          • 2013-09-02
          • 1970-01-01
          相关资源
          最近更新 更多