2013-03-10 74 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();