繁体   English   中英

PHP在循环中重复结果

[英]PHP Repeating Results in Loop

大家好,我有这个简单的问题,需要我帮忙解决。 我正在为WordPress创建一个简单的PHP短代码,其中需要3个输入audio texttotalposts 我能够创建简码的布局,但是我不知道为什么,但是它在while循环中重复了结果。

下面是代码:

function header_custom_box($atts) {

    $atts = shortcode_atts( array( 'audio' => '', 'text' => '', 'totalposts' => '3'), $atts, 'header-custom-box' );

    $audioFiles = explode( ",", $atts['audio'] ); 
    $descText = explode( ",", $atts['text'] );

    $postCount = $atts['totalposts'];
    $posts = array();
    $audioArray = array();
    $imagesArray = array();
    $textArray = array();
    $buf = '';
    $counter = 0;

    foreach ($audioFiles as $audioFile) {
        $attr = array(
        'src'      => $audioFile,
        'loop'     => '',
        'autoplay' => '',
        'preload' => 'none'
        );
        $audioArray[] = wp_audio_shortcode( $attr );
    }

    foreach ($descText as $desc) {
        $textArray[]  = $desc;
    }

    while ($counter < $postCount) { 
        $buf .= '<div class="header-tab-box">';
        $buf .= '<div class="audio-text"><h2>Your Daily Audio:</h2> <br/>';
        $buf .= $textArray[$counter];
        $buf .= '</div>';
        $buf .= '<div class="audio-player">';
        $buf .= $audioArray[$counter];
        $buf .= '</div>';
        $buf .= '</div>';
        $posts[$counter] = $buf;
        $counter++;
    }
    return $posts;

}
add_shortcode( 'header-custom-box', 'header_custom_box' );

另外,当我var_dump($posts)时它显示结果,但是如果我return $post ,它会显示Array

我还需要在此短代码中应用另一件事,这是每篇文章都有delay ,就像它应该每24到58小时更改为下一篇文章一样。

提前致谢。

这是重复的,因为您不会在每次迭代中重置$buf变量,因此在第一次迭代中$buf = "firstRow" ,在第二次迭代中$buf = "firstRowSecondRow" ...这会显示Array,因为您返回的数组$posts包含几段html。 如果要一起打印所有片段,请使用implode功能

 while ($counter < $postCount) { 
        $buf = ""; // make this empty in each iteration
        $buf .= '<div class="header-tab-box">';
        $buf .= '<div class="audio-text"><h2>Your Daily Audio:</h2> <br/>';
        $buf .= $textArray[$counter];
        $buf .= '</div>';
        $buf .= '<div class="audio-player">';
        $buf .= $audioArray[$counter];
        $buf .= '</div>';
        $buf .= '</div>';
        $posts[$counter] = $buf;
        $counter++;
    }
    return implode('',$posts);

暂无
暂无

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

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