user930026
user930026

Reputation: 1657

php return two values in loop from a function

add_shortcode( 'cpostgridelement', 'cpostgridelementFunc'  );
function cpostgridelementFunc( $atts ) {

extract(shortcode_atts(array( 'cpttypee' => 'member','templatetypee'=>'memberTemplate','postsidd'=>2 ), $atts));

// if($postsid){
    $args = array(
        'posts_per_page' => 2, // id of a page, post, or custom type
        'post_type' =>$cpttypee,
        'order'    => 'Desc',
    );
    $custom_query = new WP_Query($args);
    if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts()) :
            $custom_query->the_post();
            $title      =   get_the_title();
            $postLink   =   get_permalink();
            $f_page_images = get_field('image');
            $board_member_title = get_field('board_member_title');
            $board_member_email = get_field('email');
            $board_member_phone = get_field('phone');
            $board_member_description = get_field('board_member_description');
            $imgId = wp_get_attachment_image($f_page_images,'thumbnail');

        $collectInfoo = array();
            $collectInfoo[]="<div class='annual_cntct vc_staff_addon'>
                <div class='annual_top brd_membr'><div class='annual_top_inr stf_mmbr_inr'>
                        <div class='lft_annual_sdbr col'>".$imgId."
                        </div>
                        <div class='rght_annual_sdbr col'>
                            <h4 class='bld_anul'>".$title."</h4>
                            <h4 class='vc_gry'>".$board_member_title."</h4>
                            <h4 class='vc_gry'>".$board_member_phone."</h4>
                            <h4 class='vc_gry'>".$board_member_email."</h4>
                        </div>
                        <div class='clear'></div>
                    </div></div></div>";

                    endwhile;
                    return $collectInfoo;

    endif;
// }
// else{
    //no post id entered
    // echo "No Post Id entered";
// }
}

I am trying to get 2 posts. But the problem is if i use return it gives me just 1 post and echo can not be used here.

Upvotes: 0

Views: 93

Answers (1)

Jason Bassett
Jason Bassett

Reputation: 1291

Try this:

EDIT: I went ahead and removed the HTML you're returning in the array. Just echo the HTML in your loop when you call this function.

Although you resolved your own question, I would consider using something like this.

add_shortcode( 'cpostgridelement', 'cpostgridelementFunc'  );
function cpostgridelementFunc( $atts ) {

extract(shortcode_atts(array( 'cpttypee' => 'member','templatetypee'=>'memberTemplate','postsidd'=>2 ), $atts));

// if($postsid){
    $args = array(
        'posts_per_page' => 2, // id of a page, post, or custom type
        'post_type' =>$cpttypee,
        'order'    => 'Desc',
    );
    $custom_query = new WP_Query($args);
    if ( $custom_query->have_posts() ) :
    $collectInfoo = array();
    while ( $custom_query->have_posts()) :
        $custom_query->the_post();
        $title      =   get_the_title();
        $postLink   =   get_permalink();
        $f_page_images = get_field('image');
        $board_member_title = get_field('board_member_title');
        $board_member_email = get_field('email');
        $board_member_phone = get_field('phone');
        $board_member_description = get_field('board_member_description');
        $imgId = wp_get_attachment_image($f_page_images,'thumbnail');


       $collectInfoo[]= array("thumbnail"=>$imgId,"board_member_title"=>$board_member_title,"phone"=>$board_member_phone,"board_member_description"=>$board_member_description);
        endwhile;
        return $collectInfoo;
    endif;
// }
// else{
//no post id entered
// echo "No Post Id entered";
// }
}

Upvotes: 1

Related Questions