2013-08-18 53 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>'; 

    } 
相关问题