Wordpress - 仅从当前帖子中获取类别

ZAM*_*666 0 php wordpress post categories

我只想从当前帖子中显示 single.php 上的类别。我正在使用此代码,但它显示了所有正在使用的类别...我该怎么办?

<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>
Run Code Online (Sandbox Code Playgroud)

mbu*_*esh 5

相信您想使用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 ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
}
Run Code Online (Sandbox Code Playgroud)

文档