Iam using this wp function to show category in my post
I have certain post where i put the same post in two categories
It is showing alphabetically, I want to show first parent then child category.
EDIT:
<?php the_category(', ') ?>
Iam using this wp function to show category in my post
I have certain post where i put the same post in two categories
It is showing alphabetically, I want to show first parent then child category.
EDIT:
<?php the_category(', ') ?>
The following code will print out all the top-level categories first and then all the non-top-level categories, if that's what u wanted.
<?php
$categories = get_the_category( get_the_ID() );
if( $categories ){
$output = "";
//display all the top-level categories first
foreach ($categories as $category) {
if( !$category->parent ){
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" >' . $category->name.'</a>,';
}
}
//now, display all the child categories
foreach ($categories as $category) {
if( $category->parent ){
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" >' . $category->name.'</a>,';
}
}
echo trim( $output, "," );
}
?>
So you can use the above code instead of the_category()