matometaru
4/18/2017 - 5:15 AM

taxonomy

taxonomy

function get_news_category($taxonomy='news_cat') {
    $html;
    $terms = get_terms($taxonomy, 'orderby=order&hide_empty=1&pad_counts=1');
    $current_category = get_query_var($taxonomy);
    // 一覧のリンク生成
    $archive_link = home_url('/','relative') . 'news/';
    $html .= is_post_type_archive()
        ? '<li class="is-active"><span>すべて</span></li>'
        : '<li><a href="' . $archive_link . '">すべて</a></li>';
    // 各カテゴリのリンク生成
    foreach($terms as $term):
        $term_slug = $term->slug;
        $term_name =  $term->name;
        $term_link =  get_term_link($term->slug, $taxonomy);
        $html .= $current_category == $term_slug
            ? '<li class="is-active"><span>' . $term_name . '</span></li>'
            : '<li><a href="' . $term_link . '">' . $term_name . '</a></li>';
    endforeach;
    return $html;
}
<?php //sidebarとかのtaxonomy一覧 ループ外 ?>
<ul>
	<?php $taxonomy = 'cat_column'; ?>
	<?php $terms = get_terms($taxonomy, 'orderby=order&hide_empty=1&pad_counts=1');
	foreach($terms as $term): ?>
		<li class="<?php echo $term->slug; ?>"><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
	<?php endforeach; ?>
</ul>


<?php // taxonomyページ ループ外
	$taxonomy  = "tax_area"; //taxonomy名
	$term_slug = get_query_var($taxonomy);

	$term = get_term_by('slug', $term_slug, $taxonomy);
	$term_parent_id = $term->parent;//親termのid
	$parent_term = get_term( $term_parent_id, $taxonomy );
?>

<?php $terms = get_terms($taxonomy, $args); ?>
<?php foreach ($terms as $term) { ?>

<?php } ?>


<?php // ループ内
	$taxonomy = 'cat_column';
	$terms = get_the_terms($post->ID, $taxonomy);
	foreach($terms as $term){
		$term_slug = $term->slug;
		$term_name = $term->name;
	}
?>