将HTML添加到PHP循环
我使用以下代码输出自定义分类的列表:将HTML添加到PHP循环,php,wordpress,Php,Wordpress,我使用以下代码输出自定义分类的列表: <?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 me
<?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>';
echo'$term->name';
…但这要么会出错,要么不会打印所需的条款。我想要的html输出是:
<button class="filter term-slug" data-filter="term-slug">term-name</button>
术语名称
请用这种方法试试。它应该会起作用。您只需在HTML中这样打印“”“$term->name。”
,即可识别对象变量
<?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
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
将您的行改写如下:-
echo "<button class='filter {$term->slug}' data-filter='{$term->slug}'>$term->name</button>";
echo“$term->name”;
请添加以下代码并检查
<?php
$terms = get_the_terms( $post->ID , 'status' );
if ( $terms != null )
{
foreach( $terms as $term )
{
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
unset($term);
}
}
?>
echo应该可以工作。你能举一个例子说明你是如何使用echo做到这一点的吗?@jerodev谢谢你的评论。我已经更新了我的答案。它输出html并以静态文本的形式输出术语,它不会对它们进行操作。请修复引号。@u\u mulder,我已经修复了引号。谢谢。谢谢你的帮助:-)