我想在用户点击主要类别时显示第一个子类别

时间:2016-06-14 06:06:21

标签: php wordpress

创建一个自定义帖子类型..

我有很多类别和子类别......

category first
 -first sub cat
  --second sub cat
   ---sub cat1
   ---sub cat2
  --third sub cat
   --- sub cat1
   --- sub cat3

这是类别结构......

当用户首次点击该类别时,只看到第一个子猫,然后用户点击当时的第一个子猫,只看到存档页面中的第二个和第三个子猫....

<?php
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;;
$args=array(
    'child_of' => $term_id,
    'depth' => '1',
    'taxonomy'      => 'product-category',
    'hide_empty' => 0,
    'orderby' => 'name',
    'order' => 'ASC'
);
$categories=get_categories($args);
//print_r($categories);
?>
              <?php
            // Start the Loop.
            foreach($categories as $category) {
                 ?>
              <div class="kkp">
                <?php
                //  echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'; echo "<br>";

$product_listing_image =  get_field('product_listing_image', the_permalink());
$kpimg = $product_listing_image['sizes']['pro-img'];    
                 ?>
              </div>
              <div class="col-sm-3"> 
              <?php if($kpimg != '') { ?>
              <img src="<?php echo $kpimg; ?>" alt="" class="img-responsive">
              <?php } else { ?> <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/images/large_noImage.gif" alt="" class="img-responsive"> <?php } ?>
                <div class="venue_hire_text">
                  <h2><a href="<?php echo  get_category_link( $category->term_id ); ?>" title="<?php  sprintf( __( "View all posts in %s" ), $category->name ); ?>" ><?php echo $category->name; ?></a></h2>
                </div>
              </div>
              <?php
            }
        ?>
            </div>

1 个答案:

答案 0 :(得分:0)

这段代码对我有用

<?php 
$vnkings_cat_id = get_cat_id( single_cat_title("",false) );
$categories = get_categories(
array('child_of' => $vnkings_cat_id,'hide_empty' => 0,'orderby' => 'id', 'order' => 'ASC',)); 
if(!empty($categories)){ ?>
    <ul>
        <?php foreach($categories as $cat){
        if('Uncategorized' != $cat->name){ ?>
            <li><a  href="<?php echo get_category_link($cat->term_id); ?>"  ><?php echo $cat->name; ?></a></li>
        <?php } } ?>      
    </ul>
<?php } else { ?>
    <ul>
        <li class="cat"><a href="<?php echo get_category_link($vnkings_cat_id); ?>"><?php echo get_cat_name($vnkings_cat_id); ?></a></li>
    </ul>
<?php } ?>

教程: http://vnkings.com/hien-danh-muc-con-khi-vao-danh-muc-cha-wordpress.html

相关问题