php 在 Magento 中获取类别 URL 键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22006048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:36:48  来源:igfitidea点击:

Get category URL key in Magento

phpmagentourlmagento-1.8

提问by marchemike

How do I get the URL key of a category in Magento. I have added this text in URL key field the CMS:

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

Category-1

This is how I'm currently trying to show my category URL in an anchor:

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

But whenever I check my output it still shows like this:

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

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

I have already checked google and the magento forums for this, but I still cannot find a sufficient answer.

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

Also, is what I'm trying to call in the anchor the URL key, or is it a different URL?

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

回答by Ron

Both other answers there is a DB penalty. Best way to add Category URL info is at the collection level and simply use it to your liking in your template files. Adjust your code as follows:

其他两个答案都有 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; ?>

Notice the additional method applied to the Category Collection called addUrlRewriteToResult()and call the url by using the getUrl()instead of what you had before which was getCategoryUrl()(no such thing in the code).

请注意应用于 Category Collection 的附加方法调用addUrlRewriteToResult()并使用 url 来调用 url,getUrl()而不是使用之前的内容getCategoryUrl()(代码中没有这样的东西)。

By the way, your code should work just fine if you call getUrl()but will impact performance slightly.

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

I hope this helps.

我希望这有帮助。

回答by ThanosDi

Maybe i did not understand the question completely but the code below will give you the url of a category given the id

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

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

Simply change the id 4 inside load() with the one you need

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

回答by Giel Berkers

Using Magento (Category-) models might become very heavy to load just for loading the URL of category. When you're in a loop where you need to load the URL's of 9000+ category URL's you might consider using the url rewrite functionality to fetch the URL's since it doesn't involve loading numerous Magento models:

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

Read this articlefor more information on this one.

阅读这篇文章以获取有关此文章的更多信息。

回答by ravi patel

Use Mage::helper('catalog/category') for this

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

More info to click hear

更多信息点击收听

回答by Anil

If you want to retrieve a collection of store wise category attributes and category url , you can use

如果要检索商店明智的类别属性和类别 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'
    )
    );

And retrieve request path like $row->getStoreUrlKey() and prefix it with the store base Url. I am using this to show store specific category grid in the admin panel.

并检索像 $row->getStoreUrlKey() 这样的请求路径,并在它前面加上商店基础 Url。我正在使用它在管理面板中显示商店特定的类别网格。

回答by Jewel

<?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);