【发布时间】: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