Skip to content

Instantly share code, notes, and snippets.

@kurudrive
Last active December 28, 2015 00:09
Show Gist options
  • Save kurudrive/7411031 to your computer and use it in GitHub Desktop.
Save kurudrive/7411031 to your computer and use it in GitHub Desktop.
[WP] 親子構造を持ったタームのリンク
function term_parent_child( $taxonomy, $separator ) {
// タームの配列を取得
$terms = get_the_terms( $post->ID, $taxonomy);
// print '<pre>';print_r($terms);print '</pre>';
foreach ($terms as $term) {
if ($term->parent == 0) {
// parent が 0 のタームが先祖なのでIDを取得
$parent_term_id = $term->term_id;
} else {
// 子のターム
$child_term_id = $term->term_id;
}
}
$taxonomy_url = get_site_url().'/'.$taxonomy.'/';
$term_list = '<a href="'.esc_url($taxonomy_url.$terms[$parent_term_id]->slug).'">'.esc_html($terms[$parent_term_id]->name).'</a>';
$term_list .= $separator;
$term_list .= '<a href="'.esc_url($taxonomy_url.$terms[$child_term_id]->slug).'">'.esc_html($terms[$child_term_id]->name).'</a>';
echo $term_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment