Robert Simon Uy
Robert Simon Uy

Reputation: 43

php foreach loop echo, print, return dillema

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?

Upvotes: 0

Views: 269

Answers (2)

TJVB
TJVB

Reputation: 81

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;

Upvotes: 3

RiggsFolly
RiggsFolly

Reputation: 94662

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

Upvotes: 0

Related Questions