2016-07-30 67 views
-1

下面這個代碼獲得WordPress的自定義後的分類術語和鏈路得到一個錯誤來獲得自定義分類方面

<?php 
    $topic= get_the_terms(get_the_ID(), 'product_cat'); 
    foreach ($topic as $topics) { 
     $topiclink = $topics->name; 
     $link= get_term_link($topics, 'product_cat'); 
     echo '<a href="'.$link.'">'.$topiclink.'</a>'; 
    } 
?> 

卻發現一個錯誤「警告:提供的foreach無效參數().... 「

回答

0

確定(10第二溶液),

$topic= get_the_terms(get_the_ID(), 'product_cat'); 
if($topic){ 
    foreach ($topic as $topics) { 
     $topiclink = $topics->name; 
     $link= get_term_link($topics, 'product_cat'); 
     echo '<a href="'.$link.'">'.$topiclink.'</a>'; 
    } 
} 

閱讀文檔下一次。

https://developer.wordpress.org/reference/functions/get_the_terms/

具體是這個位。

返回#Return

(陣列|假| WP_Error)成功項的對象,假的數組,如果沒有條件或崗位不存在,WP_Error失敗。

您不能在False上循環一個布爾值。這表示沒有product_cat的條款。

+0

thx工作正常,但現在無法顯示術語名稱? – Romimitu

+0

這是一個數據問題,而不是代碼... – ArtisticPhoenix

+0

請檢查我的問題 - http://stackoverflow.com/questions/38560652/my-taxonomy-taxonomy-php-page-post-not-show – Romimitu

0

$ topic或$ topics有問題。

<?php 
$topic= get_the_terms(get_the_ID(), 'product_cat'); 
if($topic){ 
    foreach ($topic as $topics) { 
     $topiclink = $topics->name; 
     $link= get_term_link($topics, 'product_cat'); 
     echo '<a href="'.$link.'">'.$topiclink.'</a>'; 
    } 
} 
?> 
相關問題