简体   繁体   English

在foreach内部进行foreach是否正常?

[英]Is this normal for a foreach inside a foreach?

This is my code below. 这是我的下面的代码。 It uses array_chunk() to split the results into groups of three so I only have 3 columns per each .row . 它使用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>'; 
      }

The problem is with this line 问题是这条线

<h5>'.the_field('title', 'user_'.$details->ID).'</h5>

It doesn't insert the output into that <h5> tag as seen below. 不会将输出插入该<h5>标记,如下所示。

在此处输入图片说明

I've tried using double and single quotes on that line but that doesn't fix it. 我尝试在该行上使用双引号和单引号,但这并不能解决问题。

How can I fix this? 我怎样才能解决这个问题?

I suppose you are using the Advanced Custom Fields plugin . 我想您正在使用“ 高级自定义字段”插件 If you look at the docs for the_field , you'll see that it 如果查看the_field文档 ,就会看到它

Displays the value of the specified field. 显示指定字段的值。 This function is the same as echo get_field($field_name); 此函数与echo get_field($field_name);相同echo get_field($field_name);

Which is exactly what you want. 正是您想要的。 In your case, you want the function to return the field's value, not print it. 在您的情况下,您希望函数返回该字段的值,而不是打印它。 So, change your line to 因此,将您的行更改为

<h5>'.get_field('title', 'user_'.$details->ID).'</h5>

and you're good to go. 而且你很好。

The problem in your case is that you're building a string before printing it out to the browser. 您的问题是在将字符串打印到浏览器之前,您正在构建一个字符串。 During this building phase, you're calling the_field , which does its own echo ing. 在此构建阶段,您将调用the_field ,它执行自己的echo That's why the title appears before the whole div. 这就是标题出现在整个div之前的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM