2012-07-22 123 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>';