2012-01-04 91 views
2

我主頁上的限制NUMER我已經限制使用query_posts顯示最新文章的數量(「posts_per_page = 2」)的WordPress:每頁帖+排除類別

我想也排除某個類別,但能不知道如何將這兩個修改集成到循環中。

這裏是我的原代碼:

<?php query_posts('posts_per_page=2'); if (have_posts()) : while (have_posts()) : the_post();?> 

    <div class="date">Posted <?php the_time('F jS, Y') ?></div> 
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
    <?php the_content('Continue Reading →'); ?> 

<?php endwhile; endif; wp_reset_query();?> 

我試過以下幾個不同的教程,但不能讓他們的工作。我確信這是我自己的理解不足,所以對上述代碼的任何明確說明或修改都將不勝感激。

在此先感謝!

回答

3

嘗試cat=-#語法查詢:

query_posts('posts_per_page=2&cat=-1'); 

修訂展示如何手動處理它在你的循環:

<?php 
    query_posts('order=ASC'); 
    $count = 0; 
    while (have_posts()) { 
    the_post(); 
    $categories = get_the_category(); 
    if (!in_category('1')) { // category to skip 
     $count++; 
?> 
    <div class="date">Posted <?php the_time('F jS, Y') ?></div> 
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
    <?php the_content('Continue Reading →'); ?> 

<?php 
     if ($count == 2) break; 
    } 
    } 
    wp_reset_query(); 
?> 
+0

其中#是您類別的ID – paislee 2012-01-04 18:19:02

+0

@Matt H就是這樣!只是不確定如何將兩者結合起來。萬分感謝! – dadra 2012-01-04 18:22:01

+0

如果答案正確,請勾選它。謝謝 – 2012-01-04 18:56:19

0

請試試這個:

<?php 
$specified_cat = new wp_query('cat=3&posts_per_page=10'); 
while($specified_cat->have_posts()) : $specified_cat->the_post(); 
?> 
<ul> 
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> 
<ul><li><?php the_content(); ?></li> 
</ul> 
</li> 
</ul> 
<?php endwhile; ?> 
+0

OP要求排除一個類別,在這種情況下,您必須使您的類別爲-3。 – McNab 2014-06-30 09:38:35

相關問題