2015-09-15 105 views
1

我想在wordpress中創建自定義類別導航3級別。 我不想使用php wp_nav_menu(),因爲我需要在導航中添加cateogry縮略圖。 我發現這個代碼:創建自定義類別導航wordpress

<?php // Create category navigation with posts for each subcat 
    $categories = get_categories(); 
    $catID = $cat->cat_ID; 
    foreach ($categories as $cat) { 
     if($cat->category_parent == 0){ //If is a top level category 
      $subcategories = get_categories('child_of='. $cat->cat_ID); 
      if (count($subcategories)>0){ ?> 

       <li class="haschild"><a><?php echo $cat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></a> 

       <?php } /* end if has subcats */ 
      else { // else; does not have childen ?> 

       <li><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->name; ?></a><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></li> 

      <?php } //end else ?> 
     <?php // Get the subcats !!! THIS RETURNS ALL DECENDING LEVELS. 
      $subcategories = get_categories('child_of='. $cat->cat_ID); 
      if (count($subcategories)>0){ 
       echo "<ul class='sub-menu'>"; 
       foreach ($subcategories as $scat) { ?> 

       <li><a href="<?php echo get_category_link($scat->cat_ID); ?>"><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></span></a></li> 



      <?php 
      echo ""; 
      } 
      echo "</ul> </li>"; // Close the Child UL DIV and parent LI 
      } //endif has subcats ?> 

     <?php } //end if parent ?> 
    <?php } //end foreach ?> 

不過,這並不在3級的工作,只是在2級。 您可以直接在我的網站上查看菜單:http://www.designsd.fr/ap/

您有想法改變它嗎?

+0

看代碼的邏輯:它主要環路內循環來獲取子類別。只需複製關於subcats的代碼並將其放置在subcat循環中即可。 – vard

+0

是的,我知道我試圖做到這一點,但我不是開發者,只是一個前端。你能幫我寫出新循環的正確版本嗎? – SimonBak

回答

0

這裏是更新的代碼用新的循環水平:

<?php // Create category navigation with posts for each subcat 
$categories = get_categories(); 
$catID = $cat->cat_ID; 
foreach ($categories as $cat) { 
    if($cat->category_parent == 0){ //If is a top level category 
     $subcategories = get_categories('child_of='. $cat->cat_ID); 
     if (count($subcategories)>0){ ?> 

     <li class="haschild"><a><?php echo $cat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a></span> 

      <?php } /* end if has subcats */ 
     else { // else; does not have childen ?> 

      <li><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->name; ?></a><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></li> 

     <?php } //end else ?> 
    <?php // Get the subcats !!! THIS RETURNS ALL DECENDING LEVELS. 
     $subcategories = get_categories('child_of='. $cat->cat_ID); 
     if (count($subcategories)>0){ 
      echo "<ul class='sub-menu'>"; 
      foreach ($subcategories as $scat) { 
       $child_subcategories = get_categories('child_of='. $scat->cat_ID); 
       if (count($child_subcategories)>0){ ?> 
        <li class="test"><a><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></span></a> 
       <?php } else { ?> 
        <li><a href="<?php echo get_category_link($scat->cat_ID); ?>"><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></a></span> 
       <?php } 
       if (count($child_subcategories)>0){ 
        echo "<ul class='sub-menu'>"; 
        foreach ($child_subcategories as $sscat) { ?> 
         <li><a href="<?php echo get_category_link($sscat->cat_ID); ?>"><?php echo $sscat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($sscat->term_id); ?>" /></span></a></li> 
        <?php } 
        echo '</ul>'; 
       } 
       ?> 
       </li> 
       <?php; 
      } 
      echo "</ul> </li>"; // Close the Child UL DIV and parent LI 
     } //endif has subcats ?> 

    <?php } //end if parent ?> 
<?php } //end foreach ?> 
+0

非常感謝您的幫助。幾乎就是這樣。 問題持續存在 - 當您點擊具有子菜單的第二級鏈接(更多...)時,此鏈接會有一個href鏈接,以便重新加載頁面。 - 鏈接(歸檔1 /歸檔2和歸檔3)不應出現在級別2中,但第三級別(更多...) – SimonBak

+0

是否要避免鏈接到正在顯示的當前類別,或取消所有鏈接有孩子的類別? – vard

+0

取消鏈接所有類別的孩子 – SimonBak