Wordpress 如何从foreach函数返回值

Wordpress 如何从foreach函数返回值,wordpress,foreach,widget,return,shortcode,Wordpress,Foreach,Widget,Return,Shortcode,我正在为wordpress网站创建一个小部件,我需要在函数中获取多个值,并在网站上使用返回函数返回它们。如果我不使用返回码,则返回码不会出现在div中 这是我的小部件代码 if($tweets) : foreach($tweets as $t) : ?> <div class="vf-tweet"> <img src="<?php echo $t['image']; ?>" width="36"

我正在为wordpress网站创建一个小部件,我需要在函数中获取多个值,并在网站上使用返回函数返回它们。如果我不使用返回码,则返回码不会出现在div中

这是我的小部件代码

    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;
if($tweets):foreach($t)tweets:?>
“width=“36”height=“36”alt=”“/>
| 
以前


您希望使用所需的输出生成一个字符串,并返回:

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;
}
function\u name(){
$str=”“;
if($tweets):foreach($t)tweets:?>
$str=”;
/**对上述代码的其余部分执行相同的操作**/
返回$str;
}
if($tweets):foreach($t的tweets):
$tweet_show.=''.$t['text'].'124;'。人类时间差异($t['time'],当前时间('timestamp'))。'ago';
endforeach;
$tweet_show.='';
其他:
$tweet_show.=''.''No tweets found','vt translate')。

'; endif; 返回“.$tweetstitle.$tweet_show.”;
我一直试图用字符串包装它,但不知何故,我在某个地方犯了错误,所以我试图找到另一个解决方案,但运气不好,感谢您发布了您的解决方案,并让我重新审视如何构建字符串。由于小部件是用函数包装的,所以我无法再次声明函数,所以我最终得到了下面这样的解决方案,该解决方案适用于伊恩。
    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>';