繁体   English   中英

我如何在这里正确使用forloop?

[英]How do I use a forloop correctly here?

我正在为Wordpress使用推文插件,并且覆盖了其中包含推文的默认HMTL标记。 为此,我在我的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); 

这将输出以下内容:

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

我需要像这样将每条推文都包裹在自己的DIV

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

我尝试了以下方法:

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

但是,执行此操作后,仅显示1条tweet,好像在说它未正确迭代。 为什么是这样?

将最后一个for循环更改为concat,而不是在每个循环上覆盖$output 就像是:

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

尝试这个

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

暂无
暂无

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

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