Magento:从外部获取类别的相对URL [英] Magento: externally get relative URL's to categories
问题描述
我正在建立一个网站,并将Magento安装在/ shop /子目录中。我想将热门类别集成到我的非Magento网站的菜单中,以便您可以直接导航到该类别。为此,我需要类别名称和URL。
I'm building a website, and installed Magento in the /shop/ subdirectory. I'd like to integrate the top categories into the menu of my non-Magento site, so you can navigate directly into the category. For this I need the category names and url's.
Magento的类别为:
Magento's categories are:
- 模板
- 颜色
- 主题
- 常规
我首先需要获取列表类别位于模板顶层之下,因此我编写了以下代码。这是我第一次尝试与Magento集成,所以请放心:-):
I first needed to get a list of categories below the Templates top-level, so I wrote the following code. It's my first try with integrating with Magento, so be gentle :-) :
<?php require_once dirname(__FILE__).'/shop/app/Mage.php'; umask(0); Mage::app('default'); $helper = Mage::helper('catalog/category'); $collection = $helper->getStoreCategories(); foreach ($collection as $catalogArray) { if ($catalogArray->getName() == "Templates") { foreach (explode(",", $catalogArray->getChildren()) as $category) { $_category = Mage::getModel('catalog/category')->load($category); if($_category->getIsActive()) { $caturl = $_category->getUrl(); $catname = $_category->getName(); } echo "<pre>"; var_dump($caturl); echo "</pre>"; echo "<pre>"; var_dump($catname); echo "</pre>"; } } } ?>
这可以正确获取名称和类别,但是返回的URL是绝对的完整URL,例如 http://example.com/shop/templates/theme.html 。
This correctly gets the names and categories, but the URL's it returns are absolute full URL's, such as "http://example.com/shop/templates/theme.html".
首先,如何从Magento检索相对URL?
Firstly, how do I retrieve relative URL's from Magento?
第二,生成的URL似乎只是正确的如果我将web / unsecure / base_url设置设置为 http://example.com/shop/ 。如果我将其设置为{{base_url}}(由于使用基于虚拟主机的svn工作副本,我们在开发过程中会首选它),则/ shop /部分会丢失,即: http://example.com/templates/theme.html 。
Secondly, the generated URL seems to only be correct if I set the web/unsecure/base_url setting to the http://example.com/shop/. If I set this to {{base_url}} instead (which we prefer during development since we use virtual-hosting based svn working copies), the /shop/ part is missing, i.e.: "http://example.com/templates/theme.html".
知道为什么吗?
推荐答案
我认为您可以通过使用系统>配置>网络
I think you can achieve all of your requirements by playing with the settings in system > configuration > web
我要尝试的第一件事是将基本url(如果需要的话,安全和不安全)设置为/(正斜杠),然后进行系统>缓存管理>重建目录url重写
First thing I'd try is setting base url (secure and unsecure if needed) to / (forward slash), and then going system > cache management > rebuild catalog url rewrites.
如果这不起作用,请尝试进行设置,然后打赌您可以开始使用它。请注意,它将使magento在google base feed中放置相对URL等=不好!因此,请不要在生产环境中使用它。
If that doesn't work, play around with the settings, bet you can get it to work. Be warned though, it will make magento put relative urls in google base feeds etc = bad! So don't do it in production.
我的开发副本正在使用中,从这里开始是不可能的,所以不能为您尝试。
My dev copy is at work, inacessible from here, so can't try for you.
另外,对于上述脚本,您可以尝试:
Also, for your script above, you could try:
$url = "/shop/" . $_category->getUrlKey() . '.html';
(您可能不需要最后的html位,具体取决于您的配置)
(you may not need the html bit at the end, depends on your config)
这篇关于Magento:从外部获取类别的相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!