0

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?

ProEvilz
  • 5,310
  • 9
  • 44
  • 74

3 Answers3

1

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>';
}
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
  • Nevermind my past comment, that worked. Thanks! So just to be clear, it's because $output gets over written ? Would it be possible for you to link me a good resource regarding this specific thing about overwriting in a loops? I wish to get a clearer understanding in general. – ProEvilz Jun 27 '16 at 09:35
  • 1
    will be good to start from manual http://php.net/manual/en/language.operators.string.php – jitendrapurohit Jun 27 '16 at 09:42
  • 1
    Also look at some [stack overflow questions](http://stackoverflow.com/questions/124067/php-string-concatenation-performance) – jitendrapurohit Jun 27 '16 at 09:43
1

Try this

$output = '';
foreach ($items as $l)
{
    $output .= '<div class="small-4 columns">'.$l.'</div>';
}
Vinie
  • 2,983
  • 1
  • 18
  • 29
0
add_filter('latest_tweets_render_list', function( array $list ){
  $output = "";
  foreach ($list as $l) {
    $output .= "<div>{$l}</div>";
  }
  return $output;
}, 10, 1);