我想在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/
您有想法改變它嗎?
看代碼的邏輯:它主要環路內循環來獲取子類別。只需複製關於subcats的代碼並將其放置在subcat循環中即可。 – vard
是的,我知道我試圖做到這一點,但我不是開發者,只是一個前端。你能幫我寫出新循環的正確版本嗎? – SimonBak