我創建了一個稱為國籍的自定義分類法,然后我添加了它,讓用戶使用 ACF 通過自定義帳戶編輯表單選擇他/她的國籍。
我正在嘗試將 Tax 值放入 Author.php 頁面,但它回傳為 Array ,這是我正在使用的代碼:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
我還嘗試了以下代碼:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
仍然給我一個陣列。
欄位型別是選擇并且回傳值設定為術語物件或者術語 ID 錯誤然后是類 WP_Term 的物件無法轉換為字串
任何想法如何使這個正確。
謝謝!艾哈邁德
uj5u.com熱心網友回復:
您是否想從當前用戶那里獲取自定義分類法?試試這樣:
get_the_terms( get_the_ID(), 'nationality' )
如果您確定它總是只有一個(考慮到有些人有多個國籍),只需訪問第一個元素:
get_the_terms( get_the_ID(), 'nationality' )[0]
uj5u.com熱心網友回復:
我在這里有正確的代碼:
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/399199.html
