Web制作忘備録

WPカテゴリ・ターム出力

トップ MEMORANDAM WORDPRESS

WPカテゴリ・ターム出力

カテゴリ・ターム系の情報取得

カテゴリ、タームで出力方法が違う。
主にパンくず作成や、サブループで使用することが多い。

カテゴリ

<?php
// カテゴリーのデータを取得
[0]を入れないと出ない、カテゴリが複数の場合1で1番目を指定できる
$cat = get_the_category();
$cat = $cat[0];

// カテゴリー名(どちらでもok)
$cat_name = $cat->name; 
$cat_name = $cat->cat_name; 

$cat_id = $cat->cat_ID;   //ID

// スラッグ(どちらでもok)
$cat_slug = $cat->slug;
$cat_slug = $cat->category_nicename;

$cat_url = get_category_link($cat)
?>

<?php echo $cat_id;?>  //echoで関数名を入れて出力
<?php echo esc_url($cat_url); ?>  //URLの場合はこっち

ターム系

タームの場合はforeachでないと出力できない

<?php
$terms = get_the_terms($post->ID,’タクソノミスラッグ入れる’);
foreach( $terms as $term ) :?>

<?php
$t_id= $term->term_id; // ID
$t_name = $term->name; // 名前
$t_slug = $term->slug; // スラッグ
$t_link = get_term_link($term->slug,$term->taxonomy) // タームリンク

$t_group = $term->term_group; // タームグループ
$t_obj = $term->term_order; // タームオブジェクト
$t_tax_id = $term->term_taxonomy_id; // タームタクソノミーID
$t_tax = $term->taxonomy; // タクソノミー
$t_disc = $term->description; // ディスクリプション
$t_parent = $term->parent; // 親
$t_count = $term->count; // カウント
$t_obj_id = $term->object_id; // オブジェクトID
?>
<?php echo t_slug;?> //echoで関数名を入れて出力

<?php endforeach;?>