Ashley Brown Ashley Brown - 1 year ago 115
HTML Question

Is this normal for a foreach inside a foreach?

This is my code below. It uses

to split the results into groups of three so I only have 3 columns per each

$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);

//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 class="profile-details-wrap">
<h5>'.the_field('title', 'user_'.$details->ID).'</h5>
<div class="hr-wrap">
<hr class="team-hr">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam aliquam massa quis mauris sollicitudin commodo.</p>
<a class="view" href="">view profile</a>
echo '</div>';

The problem is with this line

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

It doesn't insert the output into that
tag as seen below.

enter image description here

I've tried using double and single quotes on that line but that doesn't fix it.

How can I fix this?

Answer Source

I suppose you are using the Advanced Custom Fields plugin. If you look at the docs for the_field, you'll see that it

Displays the value of the specified field. This function is the same as 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 echoing. That's why the title appears before the whole div.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download