简体   繁体   中英

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?

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>';
}

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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