Written by Anonymous
add_filter('get_the_nolink_category', function($category, $categories) {
$post_type = get_post_type();
// カスタム投稿の場合のみ処理
if ($post_type !== 'post') {
// 投稿タイプに関連付けられたタクソノミーを取得
$taxonomies = get_object_taxonomies($post_type);
// 階層型のタクソノミーのみを取得
$hierarchical_taxonomies = array_filter($taxonomies, 'is_taxonomy_hierarchical');
// 階層型タクソノミーが存在しない場合は処理を終了
if (empty($hierarchical_taxonomies)) {
return $category;
}
// 最初の階層型タクソノミーのタームを取得
$args = array(
'order' => 'ASC',
'orderby' => 'name',
);
$terms = wp_get_post_terms(get_the_ID(), reset($hierarchical_taxonomies), $args);
if (!empty($terms) && !is_wp_error($terms)) {
$term = $terms[0];
// $categoryに設定
$category = (object) array(
'cat_ID' => $term->term_id,
'cat_name' => $term->name
);
}
}
return $category;
}, 10, 2);