2012-01-04 87 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

相关问题