wordpress have function the_category('');
for show all category assigned to te current post , but i need to get current category only child and not show parent .
for example my post have category category parent --> category child and the_category; p开发者_如何学运维rint :your post cat is : ( category parent , category child )
i neeed parint , your post cat is : (category child) and not show parent .
Use get_the_category function , witch will return all categories asigned to a post ( this means all parents and childs allso ) so you can loop thru them and see witch one is parent and witch one is child and print the one you're trying to get . I suggest you build a function in you're theme functions file.
Update
For example let's say you whant to display the child category name in you're single.php theme file so you would do this:
<?php $child_category = post_child_category(get_the_ID()); ?>
<?php if ( $child_category ) echo $child_category->cat_name; ?>
In order for that to work you need to define post_child_category
function in you're theme functions file ( if you look in you're theme directory you'll see a functions.php file , if not then you can create it now ) , so you would add the following :
if ( ! function_exists( 'post_child_category' ) )
{
function post_child_category( $id = null )
{
if ( $id = null )
return false;
$categories = get_the_category( $id );
if ( count($categories) > 0 )
{
return $categories[count($categories)-1];
} else {
return false;
}
}
}
Update
If you whant to display the category link you would do this :
<?php $child_category = post_child_category(get_the_ID()); ?>
<?php if ( $child_category ) : ?>
<a href="<?php echo get_category_link($child_category->cat_ID); ?>" title="<?php echo $child_category->cat_name;?>">
<?php echo $child_category->cat_name;?>
</a>
<?php endif;?>
<ul>
<?php
$blogCategoryID = "5"; // current category ID
$childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$blogCategoryID");
if ($childCatID){
foreach ($childCatID as $kid)
{
$childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
?>
<li><a href="<?php echo get_category_link( $childCatName->term_id ); ?>"><?php echo $childCatName->name; ?></a></li>
<?php
}
}
?>
</ul>
take a look at : http://codex.wordpress.org/Function_Reference/wp_list_categories
this worked for me
$catID=$wp_query->query_vars['cat'];
$args = array('parent' => $catID);
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" ' . '>' . $category->name.'</a> </li> ';
}
精彩评论