Written by Anonymous
// タクソノミ対応カテゴリー・タグリンク
add_filter('cocoon_part__tmp/categories-tags', function($content) {
$post_type = get_post_type();
// カスタム投稿の場合
if ($post_type !== 'post') {
// 投稿タイプに関連付けられたタクソノミーを取得
$taxonomies = get_object_taxonomies($post_type);
// ターム取得
$args = array(
'order' => 'ASC',
'orderby' => 'name',
);
$terms = wp_get_post_terms(get_the_ID(), $taxonomies, $args);
if ($terms && !is_wp_error($terms)) {
$categories_html = ''; // 階層型タクソノミーのHTML
$tags_html = ''; // 非階層型タクソノミーのHTML
foreach ($terms as $term) {
// タクソノミーが階層型(カテゴリー)かどうか
if (is_taxonomy_hierarchical($term->taxonomy)) {
$categories_html .= '<a class="cat-link cat-link-' . $term->term_id . '" href="' . esc_url(get_term_link($term)) . '">
<span class="fa fa-folder cat-icon tax-icon" aria-hidden="true"></span>' . esc_html($term->name) . '</a>';
} else {
$tags_html .= '<a class="tag-link tag-link-' . $term->term_id . '" href="' . esc_url(get_term_link($term)) . '">
<span class="fa fa-tag tag-icon tax-icon" aria-hidden="true"></span>' . esc_html($term->name) . '</a>';
}
}
// カテゴリーがある場合は出力
if (!empty($categories_html)) {
$categories_html = '<div class="entry-categories">' . $categories_html . '</div>';
}
// タグがある場合は出力
if (!empty($tags_html)) {
$tags_html = '<div class="entry-tags">' . $tags_html . '</div>';
}
// 最終的なHTMLを構築
$content = '<div class="entry-categories-tags ' . esc_attr(get_additional_categories_tags_area_classes() ?: '') . '">'
. $categories_html . $tags_html . '</div>';
}
}
return $content;
});