2013-03-10 28 views
0

嗨我有一個循環,使用高級自定義字段的推薦。我需要循環一次只循環一個帖子,我曾嘗試query_posts,但它的工作。如何我使用while循環在迴路中限制帖子

<?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; 
?> 

看:

<?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 2013-03-10 13:39:36

+0

你while循環沒有使用我能看到的query_posts對象?閱讀關於WP_Query:https://codex.wordpress.org/Class_Reference/WP_Query - 它有更多的過濾參數。 – 2013-03-10 13:41:41

+0

你可以在while循環中打破 – Popnoodles 2013-03-10 13:48:47

回答

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

有與while循環有問題,你應該像這樣做。我不明白get_field做什麼,你應該通過帖子ID作爲第二個參數。

+0

'get_field'和'has_sub_field'是由高級自定義字段插件爲wordpress創建的自定義函數,它們不是WP原生的,簡而言之,它們使用'get_field'來返回值的特定領域,而且你不應該使用'query_posts'而不是'WP_Query' – 2013-03-10 14:19:39

+0

感謝您的信息,我已經更新了代碼使用WP_Query – Skatox 2013-03-10 14:27:54

+0

,因爲某種原因沒有工作 – mariovass 2013-03-10 16:16:25

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();