简体   繁体   English

如何通过类别ID号显示特定类别及其子级?

[英]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.

相关问题 WordPress:显示类别及其子类别 - Wordpress: Display category and its children WooCommerce类别选择框-显示特定类别 - WooCommerce category select box - display specific categories magento-在给定类别ID的情况下检索所有子类别 - magento - retrieve all children categories given a category id 我想在子类别上显示特定类别的名称 - I want to display on children categories name of a particular category Woocommerce店铺页面选中“显示分类”如何显示各分类ID - How to display ID of each category in Woocommerce shop page with "Show Categories" selected 显示特定类别及其子类别中的广告 - Showing ads in the specific category and its sub-categories 如何在页面上仅显示特定类别标签/名称并隐藏其他类别名称? - How to display only specific category label/name on page and hide other categories name? 如果帖子属于特定类别或其子类别,如何让WordPress使用自定义single.php模板? - How to have WordPress use a custom single.php template if the post is in a specific category OR its children? 如何在 Laravel 中按父类别递归获取所有子类别 - How to get all children categories recursively by parent category in Laravel 如何使用PHP获取父母及其子女,子女类别详细信息 - how to get the parent,and its children ,children category details using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM