2012-09-21 41 views
2

这是我的第一个Magento项目,我遇到了这样一个大问题。 我正在使用的Magento版本是1.7.0.2 我设法使用我的“app/design/frontend/default/default/template/catalog/navigation/category.phtml”中的以下代码创建自定义左栏导航栏Magento类别链接到404错误

<?php 
$cats = Mage::getModel('catalog/category')->load(1)->getChildren(); 
$catIds = explode(',',$cats); 
?> 
<div id="LeftCategory"> 
    <h2>Wicked Categories</h2> 
    <hr style="color:white; height:1px; margin:5px;" /> 
<ul> 
<?php foreach($catIds as $catId): ?> 
    <li class="Li-Top-Category"> 
     <?php 
      $category = Mage::getModel('catalog/category')->load($catId); 
      ?> 
      <a href="<?php echo $category->getUrl()?>"> 
    <?php echo $category->getName()?> 
</a> 
<?php 

      $subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren(); 
      $subCatIds = explode(',',$subCats); 
     ?> 
      <?php if(count($subCatIds) > 1):?> 
       <ul> 
       <?php foreach($subCatIds as $subCat) :?> 
        <li class="Li-Sub-Category"> 
        <?php 
         $subCategory = Mage::getModel('catalog/category')->load($subCat); 
      ?> 
      <a href="<?php echo $subCategory->getUrl()?>"> 
    <?php echo $subCategory->getName()?> 
</a> 
<?php 

        ?> 
        </li> 
       <?php endforeach;?> 
       </ul> 
      <?php endif; ?> 
    </li> 
<?php endforeach; ?> 
</ul> 

</div> 

它执行生成类别导航的工作。但问题是,每个链接指向的URL都不起作用。所有的链接都是无效的,导致404错误页面。 你可以在这里看到:http://wicked-shop.dev.thejinstudio.com/shop/catalog/category/view/s/accessories/id/15/

我已经做了这么多的搜索,没有真正解决这个问题。 我很感激你的帮助。

+0

看来你已经创建了所有这些类别作为“根类别”。根类别没有要浏览的URL键。您需要将这些类别更改为“根类别”的子类别。 –

+0

@PrasathAlbert谢谢你的观点。如何将“根类别”更改为“子类别”?我没有看到类别管理器页面中的选项..再次,我的版本是1.7.0.2 –

+0

@PrasathAlbert我做到了。只需拖放即可。该系统将工作。谢谢! –

回答

1

使用catalog/category助手从分类模型

$_catalogCatgoryHelper = Mage::helper('catalog/category'); 

得到适当的URL然后与助手您的类别传递到getCategoryUrl()功能

$_catalogCatgoryHelper->getCategoryUrl($category); 

那么试试这个。我已经把我的建议到你的代码:

<?php 
$_catalogCatgoryHelper = Mage::helper('catalog/category'); 
$cats = Mage::getModel('catalog/category')->load(1)->getChildren(); 
$catIds = explode(',',$cats); 
?> 
<div id="LeftCategory"> 
    <h2>Wicked Categories</h2> 
    <hr style="color:white; height:1px; margin:5px;" /> 
<ul> 
<?php foreach($catIds as $catId): ?> 
    <li class="Li-Top-Category"> 
     <?php 
      $category = Mage::getModel('catalog/category')->load($catId); 
      ?> 
      <a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($category) ?>"> 
    <?php echo $category->getName()?> 
</a> 
<?php 

      $subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren(); 
      $subCatIds = explode(',',$subCats); 
     ?> 
      <?php if(count($subCatIds) > 1):?> 
       <ul> 
       <?php foreach($subCatIds as $subCat) :?> 
        <li class="Li-Sub-Category"> 
        <?php 
         $subCategory = Mage::getModel('catalog/category')->load($subCat); 
      ?> 
      <a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($subCategory)?>"> 
    <?php echo $subCategory->getName()?> 
</a> 
<?php 

        ?> 
        </li> 
       <?php endforeach;?> 
       </ul> 
      <?php endif; ?> 
    </li> 
<?php endforeach; ?> 
</ul> 

</div> 

你可能会与此代码这是我在过去使用(但实际上结束了重写,使其反复显示类别,无论他们去有多深更好,研究如何做到这一点对你来说肯定是一个很好的锻炼)。这是一个有点清洁,但你需要稍微修改您的需求:

<?php $helper = $this->helper('catalog/category') ?> 
<div class="block block-categorynavigation"> 
    <div class="block-title"> 
     <strong><span><?php echo $this->__('Category') ?></span></strong> 
    </div> 
    <div class="block-content"> 
     <?php $categories = $this->getStoreCategories() ?> 
     <?php if (count($categories) > 0): ?> 
      <ul id="leftnav-tree" class="level0"> 
       <?php foreach($categories as $category): ?> 
        <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>"> 
         <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a> 
          <?php $subcategories = $category->getChildren() ?> 
          <?php if (count($subcategories) > 0): ?> 
           <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1"> 
            <?php foreach($subcategories as $subcategory): ?> 
             <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>"> 
              <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a> 
               <?php $subsubcategories = $subcategory->getChildren() ?> 
               <?php if (count($subcategories) > 0): ?> 
                <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level2"> 
                 <?php foreach($subsubcategories as $subsubcategory): ?> 
                 <li class="level2<?php if ($this->isCategoryActive($subsubcategory)): ?> active<?php endif; ?>"> 
                  <a href="<?php echo $helper->getCategoryUrl($subsubcategory) ?>"><?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?></a> 
                 </li> 
                 <?php endforeach; ?> 
                </ul> 
               <?php endif; ?> 
             </li> 
            <?php endforeach; ?> 
           </ul> 
          <?php endif; ?> 
        </li> 
       <?php endforeach; ?> 
      </ul> 
     <?php endif; ?> 
    </div> 
</div> 

编辑:原始代码只显示subcatgories为目前观看类别

+0

谢谢乔希,但你能更具体吗?像哪里可以用这个Mage :: helper和getCategoryUrl替换?我有点迷路。 –

+0

@TheJinStudio没问题,我为你修改了我的答案 –

+0

我试过了你的第一个代码。它会生成与我一样的URL。所以仍然会导致相同的404问题。对于第二个代码,我粘贴它来替换我的代码。然后这些类别不会显示出来。我想我可能需要在使用它之前先看看它。谢谢 –