dongzhao1930 2015-01-18 10:38
浏览 34

php从函数循环返回两个值

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.

  • 写回答

1条回答 默认 最新

  • dseslyh6662605 2015-01-18 10:51
    关注

    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";
    // }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助