标签: 父分类

  • WordPress调用指定父分类下的子分类

    使用循环的嵌套来实现WordPress调用指定ID父分类下的子分类,二、三级分类都可以实现。代码如下:

    <?php
    $args=array(
    'child_of'=> '666',
    'hide_empty'=>'0',
    );
    $categories=get_categories($args);
    foreach($categories as $category) {?>
     
    <h5 class="title"><a href="<?php echo get_category_link( $category->term_id ) ;?>" title="' . $category->name.'"><?php echo $category->name;?></a></h5>
    <ul>
    <?php
    $argszifenlei=array(
    'child_of'=> $category->term_id,
    'hide_empty'=>'0',
    );
    $categorieszifenlei=get_categories($argszifenlei);
    foreach($categorieszifenlei as $categoryzifenlei) {
    echo '<li ><a href="' . get_category_link( $categoryzifenlei->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $categoryzifenlei->name ) . '" ' . '>' . $categoryzifenlei->name.'</a></li>';
    }
    ?>
    </ul>';
    <?php }
    ?>
  • wordpress指定父分类调用子分类名称

    在任意位置都可以实现,指定ID父分类调用子分类名称

    <?php
    $categories=get_categories("child_of=666");
      foreach($categories as $category) {
    	echo '<li class="wodepress"><a href="'.get_category_link( $category->term_id ).'">'.$category->name.'</a></li>';
      }
    ?>
  • wordpress子分类使用父分类模板

    子分类使用父分类模板

     function f_category_template($template){
    	$category = get_queried_object();
    	if($category->parent !='0'){
    		while($category->parent !='0'){
    			$category = get_category($category->parent);
    		}
    	}
    	
    	$templates = array();
     
    	if ( $category ) {
    		$templates[] = "category-{$category->slug}.php";
    		$templates[] = "category-{$category->term_id}.php";
    	}
    	$templates[] = 'category.php';
    	return locate_template( $templates );
    }
  • wordpress父分类和归档页调用子分类名称和链接

    category和archives调用下级子分类的名称和链接

    <?php if ( is_category() ) { $this_category=g et_category( $cat ); } ?>
    <?php if ( $this_category->category_parent ) 
    $this_category = wp_list_categories( 'orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0" ); 
    else $this_category = wp_list_categories( 'orderby=id&depth=1&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0" ); 
    if ( $this_category ) { ?>
    <ul>
    <?php echo $this_category; ?>
    </ul>
    <?php } ?>
  • wordpress显示相同父分类目录下的所有子分类目录

    在制作wordpress列表模板的时候,经常会用到调用同录分类目录的情况,这段代码完美解决了这一问题。

    <?php
    $cat = get_queried_object();
    $list = wp_list_categories( [
        'taxonomy' => $cat->taxonomy,
        'child_of' => $cat->parent,
        'title_li' => '',
        'echo'     => 0,
    ] );
    if ( $list ) {
        echo "<ul>$list</ul>";
    }
    ?>
  • wordpress子分类调用父分类名称和链接

    专为导航而生,在wordpress模板制作过程中常常会在做breadcrumbs导航时会用到,子分类调用父分类的名称和链接,下面这段简洁的代码,可以完美解决这个问题。

    <?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

    下面这种方法也可以,不过代码不够简洁。

    <?php
    if ( is_category() ) {
        // Get the current category term id.
        $query_obj = get_queried_object();
        $term_id   = $query_obj->term_id;
     
        echo get_term_parents_list( $term_id, 'category' );
    }
    ?>

    第三种方法,调用分类目录名称和链接,作为导航。

    <?php
    if ( ( is_tax() || is_category() || is_tag() ) ) {
        $trail     = '';
        $home      = '/<a href="' . get_home_url() . '">Home</a>';
        $query_obj = get_queried_object();
        $term_id   = $query_obj->term_id;
        $taxonomy  = get_taxonomy( $query_obj->taxonomy );
     
        if ( $term_id && $taxonomy ) {
            // Add taxonomy label name to the trail.
           // $trail .=  '/' . $taxonomy->labels->menu_name;
            // Add term parents to the trail.
            $trail .= '/' . get_term_parents_list( $term_id, $taxonomy->name, array( 'inclusive' => false ) );
        }
     
        // Print trail and add current term name at the end.
        echo '<p class="breadcrumb-trail">' . $home . $trail . $query_obj->name . '</p>';
    }
    ?>