Wordpress - 仅从当前帖子

时间:2017-03-02 19:01:34

标签: 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>

1 个答案:

答案 0 :(得分:3)

相信你想要使用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 );
}

Documentation