タクソノミー(taxonomy-xxx.php)・投稿記事(single-xxx.php)・ループ内でタームのスラッグを取得する関数。
※投稿記事のターム選択は単一でお願いします。
Source Code
functions.php
<?php
function get_current_terms_slug($args){
$defaults = array(
'taxonomy' => '',
'loop' => false
);
$args = wp_parse_args($args, $defaults);
extract($args, EXTR_SKIP);
if(is_tax() && !$loop){
$terms = get_query_var('term');
if($terms && !is_wp_error($terms)){
$result = $terms;
}
}elseif(is_single() || $loop){
$terms = get_the_terms(0, $taxonomy);
if($terms && !is_wp_error($terms)){
$term = reset($terms);
$result = $term->slug;
}
}
if($result){
return esc_html($result);
}
}
?>
Usage
<?php get_current_terms_slug( $args ); ?>
Default Usage
<?php $args = array(
'taxonomy' => ,
'loop' => false
);
?>
Examples
archive-xxx.php/single-xxx.php
<?php echo get_current_terms_slug('taxonomy=xxxx&loop=true'); ?>