2014-12-03 63 views
11

我負責管理這個網站F9 Properties這是建立在WordPress。在主頁上有一個精選的屬性部分。我注意到如果你列出了兩個不同的「狀態」屬性,例如「For Sale or For Lease」,該屬性在輪播中出現了兩次,下面是列出特色屬性的代碼,我可以看到它過濾掉了屬性以「租借」的地位。誰能幫我添加一些代碼,列出每個職位只有一個屬性,無論它有多少種不同的財產狀況有?防止郵政複製

<?php 
/* Featured Properties Query Arguments */ 
$featured_properties_args = array(
'post_type' => 'property', 
'posts_per_page' => 100, 
'meta_query' => array(
    array(
     'key' => 'REAL_HOMES_featured', 
     'value' => 1, 
     'compare' => '=', 
     'type' => 'NUMERIC' 
    ) 
) 
); 

$featured_properties_query = new WP_Query($featured_properties_args); 

if ($featured_properties_query->have_posts()) : 
?> 
<section class="featured-properties-carousel clearfix"> 
    <?php 
    $featured_prop_title = get_option('theme_featured_prop_title'); 
    $featured_prop_text = get_option('theme_featured_prop_text'); 

    if(!empty($featured_prop_title)){ 
     ?> 
     <div class="narrative"> 
      <h3><?php echo $featured_prop_title; ?></h3> 
      <?php 
      if(!empty($featured_prop_text)){ 
       ?><p><?php echo $featured_prop_text; ?></p><?php 
      } 
      ?> 

     </div> 
     <?php 
    } 

    ?> 

     <div class="carousel es-carousel-wrapper"> 
     <div class="es-carousel"> 
      <ul class="clearfix"> 
       <?php 
       while ($featured_properties_query->have_posts()) : 
        $featured_properties_query->the_post(); 
        ?> 

        <?php 
       $status_terms = get_the_terms($post->ID,"property-status"); 
       if(!empty($status_terms)){ 
        foreach($status_terms as $status_term){ 

         if($status_term->name=="Leased"){}else{ 

          ?> 
          <li> 
         <figure> 
          <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
           <?php 
           the_post_thumbnail('property-thumb-image',array(
            'alt' => get_the_title($post->ID), 
            'title' => get_the_title($post->ID) 
           )); 
           ?> 
          </a> 
         </figure> 
         <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4> 
         <p><?php framework_excerpt(8); ?> <a href="<?php the_permalink() ?>"> <?php _e('Know More','framework'); ?> </a> </p> 
         <span class="price"><?php property_price(); ?></span> 

        </li> 
          <? 
         } 


        } 
       } 
       ?> 

        <?php 
       endwhile; 
       wp_reset_query(); 
       ?> 
      </ul> 
     </div> 
    </div> 
+0

你能分享的'$ featured_properties_query'了'WP_Query'一部分? – birgire 2014-12-08 18:57:08

回答

4

我可能會誤解你的設置,但我想知道爲什麼你要循環使用這些術語。

我認爲你應該考慮不包括leased條款WP_Query()部分(希望你可以分享它)。

那麼你的傳送帶將被簡化爲:

<div class="carousel es-carousel-wrapper"> 
    <div class="es-carousel"> 
     <ul class="clearfix"> 
     <?php while ($featured_properties_query->have_posts()) : $featured_properties_query->the_post(); ?> 
      <li><!-- YOUR POST ITEM HERE --></li> 
     <?php endwhile; ?> 
     </ul> 
    </div> 
</div> 
0

您可以將文章ID添加到陣列中的每個迭代發生,並檢查陣列,如果帖子已被渲染:

$shown = array(); // new array 
while ($featured_properties_query->have_posts()) : 
    $featured_properties_query->the_post(); 
    $status_terms = get_the_terms($post->ID, 'property-status'); 
    if(! empty($status_terms)){ 
     foreach($status_terms as $status_term){ 
      if($status_term->name == "Leased" || in_array($post->ID, $shown){ 
       continue; // post has term "Leased" or already rendered, skip 
      } 
      $shown[] = $post->ID; // add post ID to array 
     ?> 
      <!-- HTML here --> 
     <?php 
     } 
    } 
endwhile;