こんにちは
get_ancestors() を使用するのはどうでしょうか。
get_ancestors() は、指定のタームから祖先タームまでの配列を返すので、最後の配列で親の判定を、配列数で子か孫の判定をすることができます。
タームのリンクは、get_term_link() で取得することができます。
トピック投稿者
qawp
(@qawp)
いつもありがとうございます。
get_ancestors() を使用したことがありませんのでまず試してみます。
サンプルコードを書いてみました。
/**
* 指定のタームの配列の中から、指定の親ターム (ID) に属する子孫タームを返します。
*
* @param WP_Term[] $terms タームの配列。
* @param int $term_id 親ターム ID。
* @param int $depth 階層数 (親: 0, 子: 1, 孫: 2 ...)。
* @return WP_Term|false ターム。タームが見つからない場合は false を返します。
*/
function my_get_term_child( $terms, $term_id, $depth ) {
foreach ( $terms as $term ) {
$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
if ( count( $ancestors ) === $depth ) {
if ( $term_id === end( $ancestors ) ) {
return $term;
}
}
}
return false;
}
$terms = wp_get_object_terms( $post->ID, 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
if ( $term = my_get_term_child( $terms, 123, 2 ) ) {
echo '<p>場所: <a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a><p>';
}
if ( $term = my_get_term_child( $terms, 456, 1 ) ) {
echo '<p>構造: <a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a><p>';
}
}
※ コード内の 123 は場所タームの ID、456 は構造タームの ID です。
トピック投稿者
qawp
(@qawp)
ありがとうございます!
試してみたのですが、以下の行で「call stack」と出ます…。
function my_get_term_child( $terms, $term_id, $depth ) {
トピック投稿者
qawp
(@qawp)
以下のように表示されます。
この「on line 110」に該当するのが先ほどの「functon~」です。
先ほどお送りいただいたソースは<?php ?>で囲って試してみました。
よろしくお願いいたします。
Fatal error: Cannot redeclare my_get_term_child() (previously declared in C:ファイルの所在/home.php:110) in C:ファイルの所在\home.php on line 110
Call Stack
-
この返信は2年、 11ヶ月前に
qawpが編集しました。
function my_get_term_child(・・・ を複数個所に記述しているというエラーです。複数個所に記述していませんか?
下記のように my_get_term_child() の定義を functions.php に分割して記述してみてください。
functions.php に、
/**
* 指定のタームの配列の中から、指定の親ターム (ID) に属する子孫タームを返します。
*
* @param WP_Term[] $terms タームの配列。
* @param int $term_id 親ターム ID。
* @param int $depth 階層数 (親: 0, 子: 1, 孫: 2 ...)。
* @return WP_Term|false ターム。タームが見つからない場合は false を返します。
*/
function my_get_term_child( $terms, $term_id, $depth ) {
foreach ( $terms as $term ) {
$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
if ( count( $ancestors ) === $depth ) {
if ( $term_id === end( $ancestors ) ) {
return $term;
}
}
}
return false;
}
テンプレート(home.php など)に、
<?php
$terms = wp_get_object_terms( $post->ID, 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
if ( $term = my_get_term_child( $terms, 123, 2 ) ) {
echo '<p>場所: <a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a><p>';
}
if ( $term = my_get_term_child( $terms, 456, 1 ) ) {
echo '<p>構造: <a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a><p>';
}
}
?>
トピック投稿者
qawp
(@qawp)
ずいぶんと時間が開いてしまいすいません。
納期の関係で別の方法で表示できるよう対応したのですが、
あまりスマートなやり方ではないので、ishitakaさんに教えていただいたやり方で後ほど修正できればと思っています。
教えていただいた通り「functions.php」へ分けて記述しましたが、
エラーは出ませんが表示もされない状態です。
「home.php」へ
<?php ~ ?>で分けてみたのですがこちらはやはりエラーが出ます。
よろしくお願いいたします。
前回と同じエラー(Fatal error: Cannot redeclare my_get_term_child()・・・)でしょうか?
同じエラーであれば、前回書いたように my_get_term_child 関数が複数個所に記述しているというエラーです。もしかしたら既に同じ名前の関数が存在しているのかもしれません。試しに名前を変更してみてはどうでしょうか?