Wordpress - 仅从当前 post 获取类别
Wordpress - get categories only from current post
我只想在 single.php 上显示来自当前 post 的类别。我正在使用此代码,但它显示了所有正在使用的类别...我该怎么办?
<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>
相信你想使用 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 );
}
我只想在 single.php 上显示来自当前 post 的类别。我正在使用此代码,但它显示了所有正在使用的类别...我该怎么办?
<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>
相信你想使用 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 );
}