如何将 echo 函数重写为 Wordpress 主题的 return 值

How to rewrite an echo function to a return value for a Wordpress theme

我是 PHP 的新手,我很高兴到目前为止我自己编写了以下函数(它应该工作):

function get_latest_posts() {
    
    echo '<div class="latest-posts">';
        echo '<div class="brick_list">';

            $args = array(
                post_type => 'post',
                posts_per_page => 3
            );

            $latestposts_query = new WP_Query($args);

            if ( $latestposts_query->have_posts() ) : while ( $latestposts_query->have_posts() ) : $latestposts_query->the_post(); 
                
                echo ' <div ';
                    post_class("brick_item" . $termString );
                echo ' onclick="location.href=\'';
                    the_permalink();
                echo ' \'"> ';

                    echo ' <div class="brick_item-image"> ';
                        if ( has_category( 'sample' ) ) {
                            echo ' <img src="/wp-content/uploads/placeholder_1.png" /> ';                        
                        } elseif ( has_post_thumbnail() ) {
                            the_post_thumbnail(); 
                        } else {
                            echo ' <img src="/wp-content/uploads/placeholder_2.png" /> ';
                        }
                    echo ' </div> ';
                    echo ' <div class="brick_item-header"> ';
                        echo ' <h4><a href=" ';
                            the_permalink();
                        echo ' "> ';
                            the_title();
                        echo ' </a></h4> ';
                    echo ' </div> ';                        
                echo ' </div> ';
        
            endwhile; else :

                get_template_part('template_parts/content','error');

            endif; 
            wp_reset_postdata();

        echo '</div>';
    echo '</div>';
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );

但现在我确实需要你的帮助... 如您所见,我想在短代码中使用此功能。目前它首先显示在我的页面上,Google 说,这是因为我正在回应内容,我需要给出一个 return 值。

我该怎么做??它只是用“return”这个词替换“echo”这个词吗?我不知道...

为了便于阅读,我将一些陈述浓缩在一起,但它看起来像这样:

function get_latest_posts() {
    $return = '<div class="latest-posts"><div class="brick_list">';

    $args = array(
        post_type => 'post',
        posts_per_page => 3
    );

    $latestposts_query = new WP_Query($args);

    if ( $latestposts_query->have_posts() ) : while ( $latestposts_query->have_posts() ) : $latestposts_query->the_post();
        $return .= ' <div ' . post_class("brick_item" . $termString ) . ' onclick="location.href=\'' . get_permalink() . '\'"> ';

        $return .= '<div class="brick_item-image">';
        if ( has_category( 'sample' ) ) {
            $return .= '<img src="/wp-content/uploads/placeholder_1.png" />';
        } elseif ( has_post_thumbnail() ) {
            $return .= get_the_post_thumbnail();
        } else {
            $return .= '<img src="/wp-content/uploads/placeholder_2.png" />';
        }
        $return .= '</div> <div class="brick_item-header"> <h4><a href="' . get_permalink() .'">' . the_title('','',false) . '</a></h4></div></div>';

    endwhile; else :

        get_template_part('template_parts/content','error');

    endif;
    wp_reset_postdata();

    $return .= '</div></div>';
    
    return $return;
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );