[英]How to display a specific category and its children by the categories ID number?
I am using Magento and have a category under the ID 99
. 我正在使用Magento,并且在ID
99
下有一个类别。 I want to display it and its children using an HTML unordered list. 我想使用HTML无序列表显示它及其子元素。 How?
怎么样?
I copied and reformatted this code from the original question. 我从原始问题中复制并重新格式化了此代码。 Credits to Clint Hubbard Jr.
归功于Clint Hubbard Jr.
<ul>
<li>
<h2>
<a href="<?php echo Mage::getModel("catalog/category")->load(99)->getUrl() ?>">
<?php echo Mage::getModel("catalog/category")->load(99)->getName() ?>
</a>
</h2>
</li>
<?php $children = Mage::getModel('catalog/category')->getCategories(118) ?>
<?php foreach ($children as $category): ?>
<li>
<a href="<?php echo $category->getUrl(); ?>">
<?php echo $category->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Request the category model with Mage::getModel()
then load instance with ID 99 and retrieve its URL and its name. 使用
Mage::getModel()
请求类别模型,然后加载ID为99的实例并检索其URL和名称。 Then get its children in an array and go into a foreach
loop (with the PHP alternate control structure with the colon and endforeach
). 然后将其子级放入数组中,并进入
foreach
循环(使用带有冒号和endforeach
的PHP备用控件结构)。
Apparently my category model request wasn't specific enough below is the solution and fixes the problem of getting the correct url. 显然,我的类别模型请求不够具体,下面是解决方案并解决了获取正确网址的问题。
<ul class="menu-list-1">
<li>
<h2>
<a href="<?php echo Mage::getModel("catalog/category")->load(99)->getUrl() ?>">
<?php echo Mage::getModel("catalog/category")->load(99)->getName() ?>
</a>
</h2>
</li>
<?php foreach (Mage::getModel('catalog/category')->load(99)->getChildrenCategories() as $childCategory): ?>
<li>
<a href="<?php echo $childCategory->getUrl(); ?>">
<?php echo $childCategory->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.