这对于 foreach 内的 foreach 是否正常?
Is this normal for a foreach inside a foreach?
下面是我的代码。它使用 array_chunk()
将结果分成三组,因此每个 .row
我只有 3 列。
<?php
$blogusers = get_users( 'exclude=1,12'); //WordPress's get_users()
$split = array_chunk($blogusers,3);
// Array of WP_User objects.
foreach ( $split as $user ) {
echo '<div class="row">';
foreach ($user as $details){
// get user profile picture or default to a plain one
if (get_field('show', 'user_'.$details->ID)){
$img = get_field('profile_pic','user_'.$details->ID);
}
echo
//output each user
'<div class="columns small-4">
<div class="profile">
<div class="profile-image-wrap">
<img src="'.$img.'" class="team-image">
<a href="" class="profile-hover-link"><i class="fa fa-bars fa-bars fa-3x"></i></a>
</div>
<div class="profile-details-wrap">
<h4>'.$details->display_name.'</h4>
<h5>'.the_field('title', 'user_'.$details->ID).'</h5>
<div class="hr-wrap">
<hr class="team-hr">
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquam massa quis mauris sollicitudin commodo.</p>
<a class="view" href="">view profile</a>
</div>
</div>
</div>';
}
echo '</div>';
}
问题出在这一行
<h5>'.the_field('title', 'user_'.$details->ID).'</h5>
它不会将输出插入 <h5>
标记,如下所示。
我试过在该行使用双引号和单引号,但这并不能解决问题。
我该如何解决这个问题?
我想你正在使用 Advanced Custom Fields plugin. If you look at the docs for the_field
,你会看到它
Displays the value of the specified field. This function is the same as echo get_field($field_name);
这正是您想要的。在您的情况下,您希望函数 return 字段的值,而不是打印它。所以,把你的行改为
<h5>'.get_field('title', 'user_'.$details->ID).'</h5>
一切顺利。
您遇到的问题是您在将字符串打印到浏览器之前构建它。在此构建阶段,您将调用 the_field
,它自己执行 echo
ing。这就是标题出现在整个 div.
之前的原因
下面是我的代码。它使用 array_chunk()
将结果分成三组,因此每个 .row
我只有 3 列。
<?php
$blogusers = get_users( 'exclude=1,12'); //WordPress's get_users()
$split = array_chunk($blogusers,3);
// Array of WP_User objects.
foreach ( $split as $user ) {
echo '<div class="row">';
foreach ($user as $details){
// get user profile picture or default to a plain one
if (get_field('show', 'user_'.$details->ID)){
$img = get_field('profile_pic','user_'.$details->ID);
}
echo
//output each user
'<div class="columns small-4">
<div class="profile">
<div class="profile-image-wrap">
<img src="'.$img.'" class="team-image">
<a href="" class="profile-hover-link"><i class="fa fa-bars fa-bars fa-3x"></i></a>
</div>
<div class="profile-details-wrap">
<h4>'.$details->display_name.'</h4>
<h5>'.the_field('title', 'user_'.$details->ID).'</h5>
<div class="hr-wrap">
<hr class="team-hr">
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquam massa quis mauris sollicitudin commodo.</p>
<a class="view" href="">view profile</a>
</div>
</div>
</div>';
}
echo '</div>';
}
问题出在这一行
<h5>'.the_field('title', 'user_'.$details->ID).'</h5>
它不会将输出插入 <h5>
标记,如下所示。
我试过在该行使用双引号和单引号,但这并不能解决问题。
我该如何解决这个问题?
我想你正在使用 Advanced Custom Fields plugin. If you look at the docs for the_field
,你会看到它
Displays the value of the specified field. This function is the same as
echo get_field($field_name);
这正是您想要的。在您的情况下,您希望函数 return 字段的值,而不是打印它。所以,把你的行改为
<h5>'.get_field('title', 'user_'.$details->ID).'</h5>
一切顺利。
您遇到的问题是您在将字符串打印到浏览器之前构建它。在此构建阶段,您将调用 the_field
,它自己执行 echo
ing。这就是标题出现在整个 div.