2013-08-18 54 views
0

我找到並使用下面的代碼在我的網站上顯示默認分類「分類」的子分類,但我創建了自定義分類,並且無法更改代碼,以至於他正在執行對於新的分類一樣的東西,請幫助自定義分類法中的分類和子類別

<?php 

if(is_category()) { 
    $subcategories = get_terms('category', 'parent='.get_query_var('cat')); 

    if(empty($subcategories)) { 
     $thiscat = get_term(get_query_var('cat'),'category'); 
     $subcategories = get_terms('category', 'parent='.$thiscat->parent.''); 
    } 
    if(empty($subcategories)) $subcategories = array(); 
    if(!empty($subcategories)) { 
     echo '<ul>'; 

     foreach($subcategories as $subcat) { 
      if(get_query_var('cat') == $subcat->term_id) $current = ' current-cat'; else $current = ''; 
      echo ' 
      <li class="cat-item cat-item-'.$subcat->term_id.$current.'"> 
       <a href="'.get_category_link($subcat->term_id).'" title="'.$subcat->description.'">'.$subcat->name.'</a> 
      </li>'; 

     } 
     echo '</ul>'; 
    } 
} 
else { 
    // If no current cat query, just get the top level ones using wp_list_categories. 
    ?> 
    <ul> 
     <?php wp_list_categories('title_li=&depth=1');?> 
    </ul> 
    <?php 
} 
?> 
+0

這非常含糊。你需要解釋你如何改變代碼,以及你想要做什麼。請說明您嘗試過哪些更改,並解釋什麼不起作用。 –

回答

0

原來,代碼工作,因爲我想要的,但如果它可以優化我會很高興幫...這個代碼顯示的子類別的主要類別的分類。 如果有人應該使用,只需將'auto'改爲您的分類的名稱即可。我的自定義分類是'自動'

<?php 

if(is_tax()) { 
    $subcategories = get_terms('auto', 'parent='.get_queried_object()->term_id); 

    if(empty($subcategories)) { 
     $thiscat = get_term(get_queried_object()->term_id,'auto'); 
     $subcategories = get_terms('auto', 'parent='.$thiscat->parent.''); 
    } 
    if(empty($subcategories)) $subcategories = array(); 
    if(!empty($subcategories)) { 
     echo '<ul>'; 

     foreach($subcategories as $subcat) { 
      if(get_queried_object()->term_id == $subcat->term_id) $current = ' current-cat'; else $current = ''; 
      echo ' 
      <li class="cat-item cat-item-'.$subcat->term_id.$current.'"> 
       <a href="'.get_term_link($subcat,'auto').'" title="'.$subcat->description.'">'.$subcat->name.'</a> 
      </li>'; 

     } 
     echo '</ul>'; 
    } 
} 
else { 
    // If no current cat query, just get the top level ones using wp_list_categories. 
    ?> 
    <ul> 
     <?php wp_list_categories('taxonomy=auto&title_li=&depth=1');?> 
    </ul> 
    <?php 
} 
?> 
0

感謝您糾正的代碼。 我爲一些想要退出循環的人添加了一點,一旦他們到達任何目的。我沒有顯示父類別樹,而是修改了一下,以顯示該特定類別下的產品或服務的圖像和標題。以下是密碼

if(is_tax()) { 
    $subcategories = get_terms('product_categories', 'parent='.get_queried_object()->term_id); 
    // this is the change which display all the products in the specific category if it does not have child categories 
    if(empty($subcategories)) { 
     $thiscat = get_term(get_queried_object()->term_id,'product_categories'); 
    if (have_posts('thiscat')):while (have_posts('thiscat')) : the_post('thiscat');{?> 
     <div class="box cat-item-<?php $subcat->name.$current ;?>" style="min-height: 231px; margin-bottom:10px"> 
      <a href="<?php the_permalink($post->ID); ?>" title="<?php $post->name ;?>"> <?php the_post_thumbnail('thumbnail');?></a> 
     <h5><a title="<?php the_title(); ?>" href="<?php the_permalink($post->ID); ?>"><?php the_title(); ?></a></h5></h5></div> 
        <?php } 

        endwhile; 
        else : { 

           ?> 
     <div class="box cat-item cat-item-<?php $subcat->name.$current; ?>" style="min-height: 231px; margin-bottom:10px"> 
     <h5>Products will be upadated later. Do visit Later </h5 >   
     </div> 
             <?php } 


       endif; 
} 
if(empty($subcategories)) $subcategories = array(); 
if(!empty($subcategories)) { 
    echo '<div class="fixed-row clearfix dynamic-fixedRow-735" id="row_order_2">'; 

    foreach($subcategories as $subcat) { 
     if(get_queried_object()->term_id == $subcat->term_id) $current = ' current-cat'; else $current = ''; 
     echo ' 
     <div class="box cat-item cat-item-'.$subcat->name.$current.'" style="min-height: 231px; margin-bottom:10px"><h5> 
      <a href="'.get_term_link($subcat,'product_categories').'" title="'.$subcat->description.'">'.$subcat->name.'</a> 
     </h5></div>'; 

    } 
相關問題