Wordpress шорткод (php для цикла внутри html, который является insde php для каждого цикла)

Я застрял с циклом for, так как я не понимаю правильный синтаксис, мне нужно, чтобы он выполнялся между циклами html и foreach, чтобы он мог отображаться, я пытаюсь изменить шорткод в одной из моих тем wp, Я новичок в php, и у меня возникли проблемы с синтаксисом, поэтому помощь будет высоко ценится

  function testimonial( $atts, $content = null ) {
        $GLOBALS['tms_count'] = 0;
        $i = 1;
        $randomid = rand();

        do_shortcode( $content );

        if( is_array( $GLOBALS['tmss'] ) ){

            foreach( $GLOBALS['tmss'] as $tms ){

                $wrp[] = '<div>
                <div class="tm">
                    <p>'.$tms['content'].'</p>
                    <div>'.$tms['author'].'</div>
                    <div class="nonedi">
                        <div><strong>Services</strong></div>
                        <meta content="'.$tms['date'].'">'.$tms['date2'].'
                    </div>
                    <span>'.$tms['rating'].'</span>
                    Rating:<span class="star-img">'
                      /* This is where things go worng as my syntax is not right */
                        .$tms['rating'] = $test;
                    for ($x=1; $x==$test; $x++){
                        echo "<img>";
                    }
                    '</span>
                </div>';
                $i++;
            }
            $return = '<div class="nonedi">
                            <div class"name">Services</div>
                       </div>
                       <div class="testimonial-wrapper">'.implode( "\n", $wrp ).'</div>';
        }
        return $return;
    }


    function tms( $atts, $content = null) {
        extract(shortcode_atts(array(
            'author' => '',
            'date' => '',
            'date2' => '',
            'rating' => '',
        ), $atts));

        $x = $GLOBALS['tms_count'];
        $GLOBALS['tmss'][$x] = array( 'author' => sprintf( $author, $GLOBALS['tms_count'] ), 'date' => $date, 'date2' => $date2, 'rating' => $rating, 'content' =>  $content );
        $GLOBALS['tms_count']++;
    }

1 ответ

Решение

Таким образом, вы не можете запустить цикл for, как вы попробовали выше, так как он будет рассматриваться как конкатенация строк и не будет действительно выполняться и присваивать результат. Ваш цикл for также был неверным. Посмотрите здесь, чтобы узнать больше о циклах в PHP.

Я переместил ваш цикл for и присвоил только результат цикла обратно в строку. Надеюсь, это поможет.

foreach( $GLOBALS['tmss'] as $tms ){

    $img = "";
    for ($x = 1; $x <= $tms['rating']; $x++){
        $img .= "<img>";
    }

    $wrp[] = '<div>
    <div class="tm">
        <p>'.$tms['content'].'</p>
        <div>'.$tms['author'].'</div>
        <div class="nonedi">
            <div><strong>Services</strong></div>
            <meta content="'.$tms['date'].'">'.$tms['date2'].'
        </div>
        <span>'.$tms['rating'].'</span>
        Rating:<span class="star-img">'.$img.'</span>
    </div>';
    $i++;
}
Другие вопросы по тегам