2012-02-28 50 views
0

什麼,我試圖做的是運行我的索引頁面上有兩個獨立的迴路中的WordPress:WordPress的兩個循環的指數

一個不具有類職位5

而且

一說只有類別5

我一直以接近正確的結果結束,但第一個循環顯示我想要在第二個循環(這使我感到困惑)和一堆其他異常。

下面是代碼:

循環1:

<?php $blogPreview = new WP_Query('cat=-5'); ?> 
<?php if ($blogPreview->have_posts()) : ?> 
    <?php while ($blogPreview->have_posts()) : $blogPreview->the_post(); ?> 
     <?php the_post(); ?> 
     <?php get_template_part('content-index', get_post_format()); ?> 
    <?php endwhile; ?> 
<?php else : ?> 
    Sorry, but there are currently no posts in the blog! 
<?php endif; ?> 
<?php wp_reset_query(); ?> 

循環2(稍低於在我的標記環1):

<?php $testimonials = new WP_Query('cat=5'); ?> 
<?php if ($testimonials->have_posts()) : ?> 
    <?php while ($testimonials->have_posts()) : $testimonials->the_post(); ?> 
     <?php the_post(); ?> 
     <?php get_template_part('content-index-testimonials', get_post_format()); ?> 
    <?php endwhile; ?> 
<?php else : ?> 
    Sorry, but there are currently no posts in the blog! 
<?php endif; ?> 
<?php wp_reset_query(); ?> 

任何想法?在此先感謝

好吧,以便通過從循環中刪除「the_post()」來修復。

現在我不能排除此類別爲我的生活,我已經嘗試了所有的以下內容:

<?php $blogPreview = new WP_Query('cat=-5'); ?> 
<?php $blogPreview = new WP_Query(array('category__not_in'=>5)); ?> 
<?php $blogPreview = new WP_Query('category__not_in'=>array(5)); ?> 

都未能做到這一點。

+0

好吧所以原來的問題是固定的,但現在我不能爲我的生命排除類別,編輯主哨,以顯示新的問題 – BinarySolo00100 2012-02-29 16:43:40

+0

感謝您接受我的答案。至於編輯,可能值得提出一個新問題,以免兩個問題混淆。對於它的價值,我看不出你嘗試過的第一條線有什麼問題。對於第二個和第三個,您可能需要'array()'兩次,所以'$ blogPreview = new WP_Query(array('category__not_in'=> array(5)));'。無論如何,這就是我讀[codex](http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters)的方式。 – Hobo 2012-02-29 17:55:18

回答

3

不能肯定,但我懷疑它可能有一些做的調用

<?php the_post(); ?> 

你已經把它稱爲爲WP_Query對象後(如$testimonials->the_post();)。我認爲這個調用將使用全局值$wp_query,並覆蓋以前調用中設置的值。嘗試刪除它。

+0

謝謝,這工作。如果你想看看 – BinarySolo00100 2012-02-29 16:43:07

3

在兩個循環中刪除「the_post();」因爲你已經叫$ testimonials-> the_post();

<?php $testimonials = new WP_Query('cat=5'); ?> 
<?php if ($testimonials->have_posts()) : ?> 
    <?php while ($testimonials->have_posts()) : $testimonials->the_post(); ?> 
     <?php // i.e. You can call here, the_title(); ?> 
     <?php get_template_part('content-index-testimonials', get_post_format()); ?> 
    <?php endwhile; ?> 
<?php else : ?> 
    <p>Sorry, but there are currently no posts in the blog!</p> 
<?php endif; ?> 
<?php wp_reset_query(); ?> 
+0

謝謝,給了你一個加號,但是他必須先給Hobo一個答案。如果您有時間查看,請在我的評論中提出另一個問題。 – BinarySolo00100 2012-02-29 16:42:17

+0

歡迎,這是100%公平,我很欣賞,並且Hobo也值得我。 – 2012-02-29 17:07:27