Written by Anonymous
// カテゴリーリンクの取得
if ( !function_exists( 'get_the_category_links' ) ):
function get_the_category_links(){
$categories = null;
$category_list = get_the_category();
if (is_array($category_list)) {
// メインカテゴリーを先頭にする
$main_cat_id = get_the_page_main_category();
if ($main_cat_id && in_category($main_cat_id)) {
foreach ($category_list as $key => $category) {
if ($category->cat_ID == $main_cat_id) {
unset($category_list[$key]);
array_unshift($category_list, $category);
break;
}
}
}
// カテゴリーリンクを生成
foreach($category_list as $category){
$style = null;
$categories .= '<a class="cat-link cat-link-'.$category->cat_ID.'" href="'.get_category_link( $category->cat_ID ).'"'.$style.'><span class="fa fa-folder cat-icon tax-icon" aria-hidden="true"></span>'.$category->cat_name.'</a>';
}
}
return $categories;
}
endif;
// タクソノミ対応カテゴリー・タグリンク
add_filter('cocoon_part__tmp/categories-tags', function($content) {
$post_type = get_post_type();
// カスタム投稿の場合
if ($post_type !== 'post') {
// 投稿タイプに関連付けられたタクソノミーを取得
$taxonomies = get_object_taxonomies($post_type);
// タームを取得
$terms = wp_get_post_terms(get_the_ID(), $taxonomies, [
'order' => 'ASC',
'orderby' => 'name',
]);
if ($terms && !is_wp_error($terms)) {
$categories = [];
$tags = [];
// カテゴリーとタグを分類
foreach ($terms as $term) {
if (is_taxonomy_hierarchical($term->taxonomy)) {
$categories[] = $term;
} else {
$tags[] = $term;
}
}
// メインカテゴリーを先頭にする
$main_cat_id = get_the_page_main_category(get_the_ID());
if ($main_cat_id) {
foreach ($categories as $key => $term) {
if ($term->term_id == $main_cat_id) {
$main_category = $term;
unset($categories[$key]);
array_unshift($categories, $main_category);
break;
}
}
}
// カテゴリーHTMLを生成
$categories_html = '';
foreach ($categories as $term) {
$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>';
}
// タグHTMLを生成
$tags_html = '';
foreach ($tags as $term) {
$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 ($categories_html) {
$categories_html = '<div class="entry-categories">' . $categories_html . '</div>';
}
// タグがある場合は出力
if ($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;
});