NEWBIE HERE.
I'm writing this wordpress plugin so that i can use get_posts() as a shortcode.
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' );
THE PROBLEM: it outputs BEFORE the actual content. I read somewhere that this is because of ECHO, and that I need to use RETURN. If I use return, however, it breaks the loop and only one post is outputted. I also tried to use PRINT but it's just basically the same with ECHO.
My theory is that I need to RETURN the values as an ARRAY. But I don't exactly know how to do this. I tried to use the $output[] buffer but fail miserably.
Any help guys?
add_shortcodeexpects See the Manual