2015-01-18 3 views
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"; 
// } 
} 

나는 2 개의 게시물을 얻으려고합니다. 하지만 문제는 만약 내가 그것을 사용하여 하나의 게시물을 제공하고 여기에 에코를 사용할 수 없습니다 반환을 사용합니다.php 함수에서 두 개의 값을 루프로 반환합니다.

+1

'$ collectInfoo'는'while '외부에서 정의되어야합니다. –

+1

개인적으로, 나는 이제 함수에서 HTML을 반환하는 것이 좋지 않다고 생각합니다. 값을 반환하고 필요할 때 HTML을 추가해야합니다. 기능을 다시 사용해야 할 경우를 대비하여 –

답변

1

이 시도 :

편집 : 내가 나서서 당신이 배열에 반환하고있는 HTML을 제거 . 이 함수를 호출하면 루프의 HTML을 반향시킵니다.

자신 만의 질문을 해결했지만 다음과 같이 사용하는 것이 좋습니다.

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"; 
// } 
} 
+0

"Array"라는 단어를 출력합니다 – user930026

+0

이 문제를 직접 해결할 수 있습니다. changibg $ collectInfoo []를 $ collectInfoo로 변경하십시오. – user930026

+0

작동하는 한 멋지다. 그러나 앞으로 나아가 내 업데이트 된 답변을 참조하십시오. @ user930026 –

관련 문제