0

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?

3
  • Like I said I am no WP developer. Seems to me what you return depends upon understanding what add_shortcode expects See the Manual Commented Feb 11, 2016 at 9:31
  • @RiggsFolly Your first comment seem to have worked though. sorry I thought it didn't (i actually just forgot to refresh). lol. Silly me. Commented Feb 11, 2016 at 9:40
  • Oh right, I have undeleted my answer, so you can see it again. Commented Feb 11, 2016 at 9:41

2 Answers 2

3

If you change the foreach part to use a variable for the return you will have the information.

   $result = '';
    foreach( $myposts as $post ) : setup_postdata($post);
        $post_permalink = get_permalink();
        $post_title = get_the_title();
        $result .= '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';
    endforeach;
return $result;
Sign up to request clarification or add additional context in comments.

Comments

0

If return is in fact the way you must code this then just save up all the lines in a variable to be returned at the end of the process

function getposts_func($atts) {

    $htm = '';

    $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();

        $htm .= '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';

    endforeach;
    wp_reset_postdata();

    return $htm;
} 

add_shortcode( 'get_posts', 'getposts_func' );

If in fact you want it to be an array that is returned ( I am no WP expert )

function getposts_func($atts) {

    $htm = array();

    $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();

        $htm[] = '<li><a href="' . $post_permalink . '">' . $post_title . '</a></li>';

    endforeach;
    wp_reset_postdata();

    return $htm;
} 

add_shortcode( 'get_posts', 'getposts_func' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.