最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How can I only display links from a child category

programmeradmin2浏览0评论

Using this code from the codex:

       <?php
$category = get_the_category();
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

Codex link:

I am able to display the category the post is in as a link (e.g. Business). However, I don't want to display the parent categories. I only want to display the child categories of a certain parent category.

My category structure is like this: Premium (Parent) > Premium Themes (Child) > Ecommerce (Child), Business (Child), Photography (Child), etc.

So I only want to display the children of the Premium Themes category.

Any help much appreciated.

Using this code from the codex:

       <?php
$category = get_the_category();
if($category[0]){
echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
}
?>

Codex link: http://codex.wordpress/Function_Reference/get_the_category

I am able to display the category the post is in as a link (e.g. Business). However, I don't want to display the parent categories. I only want to display the child categories of a certain parent category.

My category structure is like this: Premium (Parent) > Premium Themes (Child) > Ecommerce (Child), Business (Child), Photography (Child), etc.

So I only want to display the children of the Premium Themes category.

Any help much appreciated.

Share Improve this question asked Jun 19, 2013 at 0:19 user34277user34277 53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

your code may look like

<?php
$category = get_the_category();
foreach( $category as $cat):
  if($cat[0]->parent == ' your_parent_category_id ( Premium )'){
   echo '<a href="'.get_category_link($cat[0]->term_id ).'">'.$cat[0]->cat_name.'</a>';
  break;
  }
endforeach;
?>
发布评论

评论列表(0)

  1. 暂无评论