ZAM666
ZAM666

Reputation: 31

Wordpress - get categories only from current post

I want to display the categories on single.php only from the current post. I'm using this code, but it displays all categories in use... What should I do?

<nav class="post-navigation">
<?php $args = array(
     "hide_empty" => 1,
     "type"      => "post",      
     "orderby"   => "name",
     "order"     => "ASC" );
     $categories = get_categories( $args );
    foreach ( $categories as $category ) {
        echo ' <a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>';}
?>
</nav>

Upvotes: 0

Views: 9251

Answers (1)

mburesh
mburesh

Reputation: 1028

Believe you want to be using get_the_category().

$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
    $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
}

Documentation

Upvotes: 9

Related Questions