0
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.

2
  • 1
    $collectInfoo should be defined outside while Commented Jan 18, 2015 at 10:45
  • 1
    Personally, I now think its bad to return HTML in functions. You should return values and add HTML as of when it's needed.. Just in case you need to reuse functions Commented Jan 18, 2015 at 10:50

1 Answer 1

1

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";
// }
}
Sign up to request clarification or add additional context in comments.

3 Comments

It outputs the word "Array"
I am able to resolve this myself. by changibg $collectInfoo[] to$collectInfoo.
Cool, as long as it works. But go ahead and see my updated answer as well. @user930026

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.