2013-03-10 1 views
0

안녕하세요. 고급 맞춤 입력란을 사용하여 고객 후기가있는 루프가 있습니다. 루프를 루프를 한 번에 하나의 게시물을 무작위로 내가 query_posts 시도했지만 그 doest 작업이 필요합니다. while 루프에 문제가있다루프의 게시물 제한

<?php 
       query_posts('posts_per_page=1&orderby=rand'); 
      if(get_field('testimonials', 'options')): ?> 

       <?php while(has_sub_field('testimonials', 'options')): ?> 

        <ul> 
         <li class="title"><?php the_sub_field('name'); ?></li> 
         <li class="site"><a href="<?php the_sub_field('website'); ?>" target="_blank"><?php the_sub_field('website'); ?></a></li> 
         <li class="desc"><?php the_sub_field('message'); ?></li> 
        </ul> 

       <?php endwhile; ?> 

      <?php endif; ?> 
+2

당신이 http://wordpress.stackexchange.com/ 봤어 발견? – Prix

+0

while 루프는 내가 볼 수있는 query_posts 객체를 사용하지 않습니다. WP_Query : https://codex.wordpress.org/Class_Reference/WP_Query에 대해 읽어보십시오. 필터링을위한 매개 변수가 훨씬 더 많습니다. –

+0

while 루프에서'break;'할 수 있습니다. – Popnoodles

답변

0

내가 여기 해결책 : http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

<?php 

// args 
$args = array(
    'numberposts' => -1, 
    'post_type' => 'event', 
    'meta_key' => 'location', 
    'meta_value' => 'Melbourne' 
); 

// get results 
$the_query = new WP_Query($args); 

// The Loop 
?> 
<?php if($the_query->have_posts()): ?> 
    <ul> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li> 
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
<?php endif; ?> 

<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?> 
1

,이처럼 수행해야합니다

<?php 
    $posts = new WP_Query(); 
    $posts->query('posts_per_page=1&orderby=rand'); 

    if (have_posts()) : 
     while (posts->have_posts()) : $posts->the_post(); 
      if(get_field('testimonials', 'options')): //Ain't no sure what does this ?> 
      <ul> 
       <li class="title"><?php the_sub_field('title'); ?></li> 
       <li class="site"><a href="<?php the_sub_field('website'); ?>" target="_blank"> 
       <?php the_sub_field('website'); ?></a></li> 
       <li class="desc"><?php the_sub_field('message'); ?></li> 
    </ul> 
<?php 
      endif; 
     break; // Exit loop after first post 
    endwhile; 
endif; 
?> 

봐 내가 while 루프를 사용하고 방법에 대해 설명합니다. 나는 get_field이 무엇인지 이해하지 못합니다. 두 번째 매개 변수로 게시물 ID를 전달해야합니다.

+0

'get_field'와'has_sub_field'는 WordPress의 고급 사용자 정의 필드 플러그인으로 작성된 사용자 정의 함수입니다. WP에서 기본이 아니므로'get_field'를 사용하여 값을 특정 필드, 또한'query_posts' 대신 WP_Query를 사용하십시오. –

+0

정보를 주셔서 감사합니다. WP_Query를 사용하여 코드를 업데이트했습니다. – Skatox

+0

어떤 이유로 작동하지 않습니다. – mariovass

0

페이지 당 하나 개의 게시물을 반복이 시도 :

$args = array(
    'posts_per_page' => 1, 
    'orderby' => 'rand' 
    ); 
$the_query = new WP_Query($args); 


while ($the_query->have_posts()) : 
    $the_query->the_post(); 
    echo '<ul>'; 
    echo '<li>' . get_the_title() . '</li>'; 
    echo '</ul>'; 
    echo '<li class="title">'.the_sub_field('name'). '</li>'; 
    echo '<li class="site"><a href="'.the_sub_field('website').'" target="_blank">'.the_sub_field('website').'</a></li>'; 
    echo '<li class="desc">'.the_sub_field('message').'</li>'; 
endwhile; 


wp_reset_postdata(); 
관련 문제