【问题标题】:How to rewrite an echo function to a return value for a Wordpress theme如何将回显函数重写为 Wordpress 主题的返回值
【发布时间】:2021-06-14 02:11:31
【问题描述】:

我是 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' );

但现在我绝对需要你的帮助... 如您所见,我想在简码中使用此功能。目前它首先显示在我的页面上,谷歌说,这是因为我正在回显内容,我需要给出一个返回值。

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

【问题讨论】:

  • 将所有内容连接到一个变量中并在函数末尾返回该变量

标签: php return wordpress-theming echo


【解决方案1】:

为了便于阅读,我将一些语句压缩在一起,但看起来是这样的:

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' );

【讨论】:

  • 我认为 the_permalink()the_title() 回应了他们的结果。 (They do)
  • 好电话!一分钟,让我解决这个问题
  • the_post_thumbnail() 相同。使用 get_ 前缀应该可以解决这个问题
  • 哦 WordPress ...不断给予的礼物
  • 这就是为什么我想分享@brombeer 和我一起工作的完整代码。我认为继续学习和提出问题很重要。我不会担心任何事情的反对票。我通常每天要回答 2-3 次,但有人不喜欢我说的话并给我投了反对票。我来这里是为了帮助和吹毛求疵我说的东西没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多