嗨我有一個循環,使用高級自定義字段的推薦。我需要循環一次只循環一個帖子,我曾嘗試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; ?>
你試過http://wordpress.stackexchange.com/? – Prix 2013-03-10 13:39:36
你while循環沒有使用我能看到的query_posts對象?閱讀關於WP_Query:https://codex.wordpress.org/Class_Reference/WP_Query - 它有更多的過濾參數。 – 2013-03-10 13:41:41
你可以在while循環中打破 – Popnoodles 2013-03-10 13:48:47