簡體   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