2012-07-22 112 views
0

爲wordpress網站創建一個窗口小部件,我需要在函數中獲取多個值,並在網站上返回它們的返回值。如果我不使用返回代碼不會出現內部div的如何從foreach函數返回值

這裏是我的小部件

if($tweets) : foreach($tweets as $t) : ?> 
      <div class="vf-tweet"> 
       <img src="<?php echo $t['image']; ?>" width="36" height="36" alt="" />     
       <div class="vf-tweet-inner"> 
        <?php echo $t['text']; ?> | 
         <span class="vf-tweet-time"><?php echo human_time_diff($t['time'], current_time('timestamp')); ?> ago</span> 
       </div><!-- /vf-tweet-inner --> 
      </div> 
     <?php endforeach; ?> 
     <a href="http://twitter.com/#!/<?php echo $name; ?>">[ Follow us on Twitter ]</a> 
    <?php else : ?> 
     <p><?php _e('No tweets found.', 'vt-translate') ?></p> 
    <?php endif; 

代碼,我需要使用恢復功能,但即時通訊有點卡住我不知道如何來處理這個退貨。

回答

1

你想建立一個字符串你所需的輸出,並返回:

function function_name() { 
    $str = ""; 

    if($tweets) : foreach($tweets as $t) : ?> 
     $str .= "<div class="vf-tweet">"; 
     /** Do the same for the rest of the code above **/ 

    return $str; 
} 
+0

我一直在試圖把它包起來的字符串,但不知何故,我是會犯錯的地方,所以我試圖找到另一種解決方案但沒有運氣,感謝您發佈您的解決方案,並讓我再次考慮構建字符串。由於小部件被封裝在函數中,我不能再次聲明函數,所以我結束了像下面的解決方案,工作正常。 – 2012-07-23 23:50:54

0
if($tweets) : foreach($tweets as $t) : 
     $tweet_show .= '<div class="vf-tweet"><img src='. $t['image'].' width="36" height="36" alt="" /><div class="vf-tweet-inner">'.$t['text'].' | <span class="vf-tweet-time">'. human_time_diff($t['time'], current_time('timestamp')).' ago</span></div></div>'; 
     endforeach; 
     $tweet_show .= '<a href="http://twitter.com/#!/'. $name .'">[ Follow us on Twitter ]</a>'; 
    else : 
     $tweet_show .= '<p>'. __('No tweets found.', 'vt-translate') .'</p>'; 
    endif; 

return '<div class="vf-tweets">'.$tweetstitle . $tweet_show .'</div>';