2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

タームのスラッグを取得する

Last updated at Posted at 2012-08-03

タクソノミー(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'); ?>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?