カテゴリー一覧の取得(リンク付き)
<?php
$terms = get_terms('category');
foreach ($terms as $term) {
echo '<a href="' . get_term_link($term->slug, 'category') . '">' . $term->name . '</a>';
}
?>
タグ一覧の取得(リンク付き)
<?php
$tags = get_terms('post_tag');
foreach ($tags as $tag) {
echo '<a href="' . get_term_link($tag->slug, 'post_tag') . '">' . $tag->name . '</a>';
}
?>
記事に属する全てのカテゴリーの取得(リンク付き)
<?php
$terms = get_the_terms($post->ID, 'category');
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
echo '<a href="' . esc_url(get_term_link($term->slug, 'category')) . '" class="link">' . esc_html($term->name) . '</a>';
}
}
?>