我試圖顯示自定義文章類型的分類術語(如在常規文章中使用<?php the_category(' '); ?>
)。下面的代碼工作,但需要指定分類名稱,有沒有辦法只使用郵政ID?獲取沒有分類名稱的分類術語
<?php
$terms = get_the_terms($post->ID , 'taxonomy_name');
foreach ($terms as $term) {
echo $term->name;
}
?>
在此先感謝!
我試圖顯示自定義文章類型的分類術語(如在常規文章中使用<?php the_category(' '); ?>
)。下面的代碼工作,但需要指定分類名稱,有沒有辦法只使用郵政ID?獲取沒有分類名稱的分類術語
<?php
$terms = get_the_terms($post->ID , 'taxonomy_name');
foreach ($terms as $term) {
echo $term->name;
}
?>
在此先感謝!
<?php print the_terms($post->ID, 'taxonomy_name' , ' '); ?>
您無法獲得沒有分類名稱的自定義分類術語,但上面的代碼對您而言較短。
我找到了一個辦法。使用「get_post_taxonomies」,並選擇陣列巫貓[1]
<?php
$cat = get_post_taxonomies($post);
$terms = get_the_terms($post->ID , $cat[1]);
// Loop
if ($terms != null){
foreach($terms as $term) {
$term_link = get_term_link($term, $cat[1]);
// Print the name and URL
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
unset($term); } }
?>
試試這個,
您必須使用分類不分類不能得到長期的細節。
謝謝你!
嗨@Gazi,謝謝你的回答。我找到了一個辦法。在foreach循環之前使用它:$ cat = get_post_taxonomies($ post); $ terms = get_the_terms($ post-> ID,$ cat [1]); –