
今天在做一款企业主题,想实现上面这种 调用出当前父分类下的所有子分类
下面是代码,放在functions.php中:
function get_category_root_id($cat) {
// 取得当前分类
$this_category = get_category($cat);
// 若当前分类有上级分类时循环
while($this_category->category_parent) {
// 将当前分类设为上级分类
$this_category = get_category($this_category->category_parent);
}
// 返回根分类的id号
return $this_category->term_id;
}
下面是调用代码,列出所有子分类
<?php wp_list_categories('child_of='.get_category_root_id($cat).'&depth=1&hide_empty=0&hierarchical=1&optioncount=1&title_li=');?>
但是并不能调用当前的父分类名和链接,网上找了半天教程结果只有调用出当前的子分类,并不包括父分类,开始测试了用调用当前分类的调用代码来实现,但是点击子分类的时候,父分类就不显示了
最后自己想了个办法,当点击子分类的时候怎么显示当前父分类呢?代码如下:
<a href="<?php echo get_category_link(get_category_root_id($cat));?>"><?php echo get_cat_name(get_category_root_id($cat));?></a>
这个代码实现方法也很简单。
看了其他主题的代码,实现效果是下面这样的,但是我使用不知道什么原因,调用不出,而且代码太复杂了。所以也弃用了。
<?php if ( is_page() ) { ?>
<?php
$parent_page = $post->ID;
while($parent_page) {
$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$parent_page'");
$parent_page = $page_query->post_parent;
}
$parent_id = $page_query->ID;
$parent_title = $page_query->post_title;
if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
<?php $subpage = wp_list_pages('depth=1&echo=0&child_of='.$parent_id); ?>
<?php //key ?>
<?php if($subpage) { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<?php wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
<?php } else { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<li class=""><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
<?php } ?>
<!-- SubPageList end -->
<?php } elseif( is_search() || is_404() ) { ?>
<?php } else { ?>
<?php
$this_category = get_the_category();
$category_id = $this_category[0]->cat_ID;
$parent_id = get_category_root_id( $category_id );
$category_link = get_category_link( $parent_id );
$childcat = get_categories('child_of='.$parent_id);
if( $childcat && $parent_id ){
?>
<!-- SubCatList begin -->
<div class="page-menu-title"><h3><a href="<?php echo get_category_link( $parent_id ); ?>" title=" <?php echo get_cat_name( $parent_id ); ?>" style="color: #FFF;"><?php echo get_cat_name( $parent_id ); ?></a></h3></div>
<?php wp_list_cats("orderby=id&child_of=" . $parent_id . "&depth=2&hide_empty=0"); ?>
<!-- SubCatList end -->
<?php } else { ?>
<div class="page-menu-title"><h3><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h3></div>
<li class=""><?php the_category(', ') ?></li>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</section>
</section><section class="page-menu">
<div class="page-menu-content">
<ul>
<?php if ( is_page() ) { ?>
<!-- SubPageList begin -->
<?php
$parent_page = $post->ID;
while($parent_page) {
$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$parent_page'");
$parent_page = $page_query->post_parent;
}
$parent_id = $page_query->ID;
$parent_title = $page_query->post_title;
if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
<?php $subpage = wp_list_pages('depth=1&echo=0&child_of='.$parent_id); ?>
<?php //key ?>
<?php if($subpage) { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<?php wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
<?php } else { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<li class=""><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
<?php } ?>
<!-- SubPageList end -->
<?php } elseif( is_search() || is_404() ) { ?>
<?php } else { ?>
<?php
$this_category = get_the_category();
$category_id = $this_category[0]->cat_ID;
$parent_id = get_category_root_id( $category_id );
$category_link = get_category_link( $parent_id );
$childcat = get_categories('child_of='.$parent_id);
if( $childcat && $parent_id ){
?>
<!-- SubCatList begin -->
<div class="page-menu-title"><h3><a href="<?php echo get_category_link( $parent_id ); ?>" title=" <?php echo get_cat_name( $parent_id ); ?>" style="color: #FFF;"><?php echo get_cat_name( $parent_id ); ?></a></h3></div>
<?php wp_list_cats("orderby=id&child_of=" . $parent_id . "&depth=2&hide_empty=0"); ?>
<!-- SubCatList end -->
<?php } else { ?>
<div class="page-menu-title"><h3><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h3></div>
<li class=""><?php the_category(', ') ?></li>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</section><?php if ( is_page() ) { ?>
<?php
$parent_page = $post->ID;
while($parent_page) {
$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$parent_page'");
$parent_page = $page_query->post_parent;
}
$parent_id = $page_query->ID;
$parent_title = $page_query->post_title;
if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
<?php $subpage = wp_list_pages('depth=1&echo=0&child_of='.$parent_id); ?>
<?php //key ?>
<?php if($subpage) { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<?php wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
<?php } else { ?>
<div class="page-menu-title"><h3><?php echo $parent_title; ?></h3></div>
<li class=""><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
<?php } ?>
<!-- SubPageList end -->
<?php } elseif( is_search() || is_404() ) { ?>
<?php } else { ?>
<?php
$this_category = get_the_category();
$category_id = $this_category[0]->cat_ID;
$parent_id = get_category_root_id( $category_id );
$category_link = get_category_link( $parent_id );
$childcat = get_categories('child_of='.$parent_id);
if( $childcat && $parent_id ){
?>
<!-- SubCatList begin -->
<div class="page-menu-title"><h3><a href="<?php echo get_category_link( $parent_id ); ?>" title=" <?php echo get_cat_name( $parent_id ); ?>" style="color: #FFF;"><?php echo get_cat_name( $parent_id ); ?></a></h3></div>
<?php wp_list_cats("orderby=id&child_of=" . $parent_id . "&depth=2&hide_empty=0"); ?>
<!-- SubCatList end -->
<?php } else { ?>
<div class="page-menu-title"><h3><?php $category = get_the_category(); echo $category[0]->cat_name; ?></h3></div>
<li class=""><?php the_category(', ') ?></li>
<?php } ?>
<?php } ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</section>
</section>