这里的代码用于获取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()提供的参数无效"
Ok(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/
特别是这个比特。
退货#退货
(array|false|WP_Error)成功时的术语对象数组,如果没有术语或帖子不存在则为false,失败时为WP_Error。
不能在False
上循环布尔值。这将表明不存在CCD_ 2的项。
$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>';
}
}
?>