LoginSignup
0
0

More than 5 years have passed since last update.

Custom Taxsonomy の色々

Last updated at Posted at 2018-05-07

taxonomyページの一覧テンプレート

taxonomy-area-tokyo.php
taxonomy-area.php
taxonomy.php
archive.php
index.php

taxonomyページのオブジェクト表示

qiita.rb
$term_object = get_queried_object();
var_dump($term_object);

***

object(WP_Term)[4405]
  public 'term_id' => int 20
  public 'name' => string 'belt' (length=4)
  public 'slug' => string 'belt' (length=4)
  public 'term_group' => int 0
  public 'term_taxonomy_id' => int 20
  public 'taxonomy' => string 'gruff_bark' (length=10)
  public 'description' => string '' (length=0)
  public 'parent' => int 0
  public 'count' => int 2
  public 'filter' => string 'raw' (length=3)

single.phpで属するtaxonomyを取得

色々あるけどこれがいいかも

qiita.rb
$tax = get_the_taxonomies()

***

array (size=1)
  'arr' => string 'Arr: <a href="http://test.dev/orders/arr/belt">belt</a>。' (length=77)

***

var_dump(key(array_slice($tax, 0, 1, true)));

termを調べる場合はそのまま下記で

qiita.rb
$post_term = get_the_terms($post->ID, $first_tax_key);

***
array (size=1)
  0 => 
    object(WP_Term)[4438]
      public 'term_id' => int 20
      public 'name' => string 'belt' (length=4)
      public 'slug' => string 'belt' (length=4)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 20
      public 'taxonomy' => string 'arr' (length=10)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 5
      public 'filter' => string 'raw' (length=3)
***

$post_term[0]->name;

最初からget_the_term()で良くない?と思いがちだけど
get_the_terms( $id, $taxonomy );は taxonomyが必須になっている
複数のtaxonomyを運用していて、POSTがどのtaxonomyに属しているか調べるには上記が必要

taxonomyページ条件判定

qiita.rb
<?php is_tax( $taxonomy, $term ); ?>

特定のtermの記事だけ取得する時の$argsの書き方

qiita.rb
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => 'jazz'
        )
    )
);
$postslist = get_posts( $args );
0
0
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
0
0