2014-12-03 19 views
11

Tôi chịu trách nhiệm quản lý trang web này F9 Properties được xây dựng trong WordPress. Trên trang chủ có phần thuộc tính nổi bật. Tôi nhận thấy rằng nếu bạn liệt kê một thuộc tính có hai "Trạng thái" khác nhau, chẳng hạn như "Để bán hoặc cho thuê, tài sản xuất hiện hai lần trong băng chuyền. Dưới đây là mã để liệt kê các thuộc tính nổi bật. Tôi có thể thấy rằng nó lọc ra các thuộc tính với trạng thái "Đã cho thuê". Bất kỳ ai cũng có thể giúp tôi thêm một chút mã để chỉ liệt kê một thuộc tính cho mỗi bài đăng bất kể có bao nhiêu trạng thái thuộc tính khác nhau không?Ngăn bài đăng trùng lặp

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

Bạn có thể chia sẻ 'WP_Query' phần cho môi trường '$ featured_properties_query'? – birgire

Trả lời

4

Tôi có thể hiểu nhầm thiết lập của bạn, nhưng tôi tự hỏi tại sao bạn lặp lại các điều khoản.

Tôi nghĩ bạn nên xem xét trừ thời hạn leased trong phần WP_Query() (hy vọng bạn có thể chia sẻ nó).

Sau đó, băng chuyền của bạn sẽ được đơn giản hóa để:

<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

Bạn có thể thêm ID bài đăng vào một mảng mỗi thời gian lặp lại xảy ra và kiểm tra mảng nếu bài đăng đã được hiển thị:

$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; 
Các vấn đề liên quan