2016-07-20 24 views
2

我已經閱讀了WordPress上的多重循環codex,但是我仍然對如何做到這一點感到困惑。WordPress - 你如何合併兩個或更多的循環?

我必須從默認查詢運行3個查詢。

-1 =最新的職位從X類

-2 = Y的類別

-3所有帖子= X類,除了最近的所有帖子(因爲它在-1拉)

這是怎樣我現在有我的index.php設置:

<?php 

get_header(); ?> 

    <?php 
     $args = array(
      'posts_per_page' => 1, 
      'category_name' => 'news', 
     ); 
     $news_query = new WP_Query($args); 
     if ($news_query->have_posts()) : 
      ?> 

      <?php while ($news_query->have_posts()) : $news_query->the_post(); ?> 

       <?php 
        $featured_img = get_the_post_thumbnail_url($post->ID, 'full'); 
        $date = Date('m/y'); 
        $title = get_the_title($post->ID); 
        $link = get_the_permalink($post->ID);    
       ?> 

       <?php echo '<div class="recent-news-banner" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; ?> 

       <?php echo '<div class="container"><div class="row"><div class="col-md-8"><div class="' . 'entry-meta' . '"><div class="meta-date">' . $date . '</div><div class="meta-info"><div class="meta-title"><h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2><div class="meta-excerpt">' . get_the_excerpt() . '</div></div></div></div></div></div>'; ?> 

       <?php echo '</div>'; ?> 

      <?php endwhile;?> 

     <?php else : ?> 
      <p><?php _e("No News Found", 'news'); ?></p> 
     <?php wp_reset_postdata(); ?> 
     <?php endif 
    ?> 

    <div id="primary" class="content-area"> 
     <main id="main" class="site-main" role="main"> 

     <?php 
      $args = array(
       'posts_per_page' => -1, 
       'category_name' => 'events', 
      ); 
      $events_query = new WP_Query($args); 

      echo '<div id="owl-testimonials" class="owl-theme owl-carousel container"><div class="row"><div class="col-xs-12">'; 

      if ($events_query->have_posts()) : 
       ?> 

       <?php while ($events_query->have_posts()) : $events_query->the_post(); ?> 

        <?php 
         $featured_img = get_the_post_thumbnail_url($post->ID, 'full'); 
         $date = Date('F j, Y'); 
         $title = get_the_title($post->ID); 
         $link = get_the_permalink($post->ID);    
        ?> 

        <?php echo '<div class="event-item" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; ?> 

        <?php echo '<div class="meta-date">' . $date . '</div>'; ?> 

        <?php echo '</div>'; ?> 

        <?php echo '<div class="meta-info"><div class="meta-title"><h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3></div></div>'; ?> 

       <?php endwhile;?> 

       <?php echo '</div></div></div>'; ?> 

      <?php else : ?> 
       <p><?php _e("No News Found", 'news'); ?></p> 
      <?php wp_reset_postdata(); ?> 
      <?php endif 
     ?> 

     </main><!-- #main --> 
    </div><!-- #primary --> 

<?php 
get_footer(); 

我寫出來作爲兩個wp_queries但我,第二個將不加載。因爲這個,我沒有嘗試第三個。首先是默認循環wp_query罰款?其次,我該如何重做這件事,以便它可以使用多於循環。

+0

生成的內容出現在源代碼中,但似乎在'.owl-carousel'類中有'display:none'樣式 - 你是否錯過了一些init? – Will

+0

看到源代碼的另一個問題是'#primary'在'.recent-news-banner'裏面,但是我關閉了那個div,所以不知道這裏發生了什麼。我認爲這是如何編寫循環。 –

+0

我在你的第一個循環中的第二個回聲中打開8個div打開,但只有7個關閉。其實,你有沒有理由做回聲呢?你可以將這些div內聯,這樣你就可以格式化和縮進它們,以便更容易地看到發生了什麼...... – Will

回答

0

你的代碼有幾個問題,但我不相信循環是不正確的。我能夠看到頁面源中第二個循環中生成的內容;貓頭鷹旋轉木馬似乎沒有被初始化。

問題1:沒有被分配到$,所以$('#owl-events').owlCarousel(...)電話不工作(事實上,它看起來並不像什麼在main.js工作你需要要麼讓jQuery的接管。 $或明確地使用jQuery,而不是$

問題2:。未關閉的divs在第一循環時重複了9個div但靠近其中只有8這是很容易堆積全部將它們放在一個時錯過你不需要echo那個內容,你可以直接內聯他們在頁面上,並在需要的地方散佈php,並保持一些格式,以便您可以看到發生了什麼。例如:

... 
<?php while ($news_query->have_posts()) : $news_query->the_post(); ?> 
... 
    <div class="recent-news-banner" style="background: url(<?= $featured_img ?>') no-repeat center center; background-size: cover;">'; ?> 
    <div class="container"> 
     <div class="row"> 
     <div class="col-md-8"> 
      <div class="entry-meta"> 
      <div class="meta-date"><?= $date ?></div> 
      <div class="meta-info"> 
       <div class="meta-title"> 
       <h2> 
        <a href="<?= get_permalink() ?>"><?= get_the_title() ?></a> 
       </h2> 
       <div class="meta-excerpt"><?= get_the_excerpt() ?></div> 
       </div> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    <!-- uh oh, not enough closing divs --> 
<?php endwhile;?> 
... 

可能還有其他的問題,但是這兩個應該給你一個起點,讓你(和我們)看到任何別的東西,可能是背後的原因。

相關問題