2016-07-30 70 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>'; 
    } 
} 
?> 
相关问题