Ashley Brown Ashley Brown - 5 months ago 6
HTML Question

How do I use a forloop correctly here?

I am using a tweets plugin for Wordpress and I'm overriding the default HMTL markup that the tweets are contained within. For this, I'm using a filter in my functions.php.

add_filter('latest_tweets_render_list', function( array $list ){
$output = '<div>';
foreach ($list as $l) {
$output .= $l;
}
$output .= '</div>';
return $output;
}, 10, 1);


This will output the following:

<div>
//tweets
//tweets
//tweets
//tweets
</div>


I need each tweet to be wrapped in it's own
DIV
like this

<div>
//tweet
</div>
<div>
//tweet
</div>


I tried the following:

add_filter( 'latest_tweets_render_list', function( array $items, $screen_name=null ){

foreach ($items as $l) {
$output = '<div class="small-4 columns">'.$l.'</div>';
}
return $output;

}, 10 , 1 );

add_filter( 'latest_tweets_render_list', function( array $items, $screen_name=null ){

foreach ($items as $l) {
echo '<div class="small-4 columns">'.$l.'</div>';
}

}, 10 , 1 );


However, after doing this, only 1 tweet is displayed as if to say it didn't iterate through properly. Why is this?

Answer

Change your last for loop to concat instead of overriding $output on each loop. Something like:

foreach ($items as $l) {
  $output .= '<div class="small-4 columns">'.$l.'</div>';
}