2 回答

TA貢獻1799條經驗 獲得超6個贊
您的問題對我來說有點不清楚,但是您似乎想要所有類別的列表,對嗎?我認為“$categories = get_the_category();”這一行 僅獲取當前(在本例中為第一個)帖子的類別。
如果您想要博客/網站中存在的所有類別的列表,請嘗試“get_categories”,https://developer.wordpress.org/reference/functions/get_categories/

TA貢獻1815條經驗 獲得超6個贊
試試這個代碼,對你的代碼做些小改動......
<?php if (have_posts()) : $i = 1; while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
<div class="categories">
<?php
if( $i == 1){
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
?>
</div>
<?php $i++; endwhile; ?>
- 2 回答
- 0 關注
- 144 瀏覽
添加回答
舉報