我使用下面的代碼輸出自定義分類的列表:添加HTML到PHP循環
<?php // Get terms for post
$terms = get_the_terms($post->ID , 'status');
// Loop over each item since it's an array
if ($terms != null){
foreach($terms as $term) {
// Print the name method from $term which is an OBJECT
print $term->slug ;
print $term->name;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
我的問題是,我怎麼可以添加HTML這個循環?我已經嘗試了多種方法,例如:
echo '<button class="filter $term->slug" data-filter="$term->slug">$term->name</button>';
...但是,這可能會出錯或不打印所需的條款。我希望的HTML輸出將是:
<button class="filter term-slug" data-filter="term-slug">term-name</button>
在此先感謝。
迴應應該工作。你能舉一個例子說明你如何使用echo來做到這一點? – Jerodev
@jerodev感謝您的評論。我已經更新了我的答案。它輸出html並將這些項輸出爲靜態文本,但不會對它們進行操作。 – CharlyAnderson