【发布时间】:2016-05-21 23:35:39
【问题描述】:
这里是新手。
我正在编写这个 wordpress 插件,以便我可以使用 get_posts() 作为简码。
function getposts_func($atts) {
$atts = shortcode_atts( array('category' => '',), $atts, 'get_posts' );
$cat=$atts['category'];
global $post;
$args = array(
'category' => $cat,
'numberposts' => -1,
'order' => 'ASC',
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$post_permalink = get_permalink();
$post_title = get_the_title();
echo '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
endforeach;
wp_reset_postdata();} add_shortcode( 'get_posts', 'getposts_func' );
问题:它在实际内容之前输出。我在某处读到这是因为 ECHO,我需要使用 RETURN。但是,如果我使用 return,它会中断循环并且只输出一篇文章。我也尝试使用 PRINT,但它与 ECHO 基本相同。
我的理论是我需要将值作为数组返回。但我不完全知道如何做到这一点。我尝试使用 $output[] 缓冲区,但失败得很惨。
有帮助吗?
【问题讨论】:
-
就像我说的我不是 WP 开发人员。在我看来,您返回的内容取决于了解
add_shortcode的期望 See the Manual -
@RiggsFolly 您的第一条评论似乎奏效了。抱歉,我认为它没有(实际上我只是忘了刷新)。哈哈。傻我。
-
哦,对了,我已经取消删除我的答案,所以你可以再看一遍。
标签: php wordpress foreach return echo