【问题标题】:Wordpress: Limit numer of posts per page + exclude categoryWordpress:限制每页的帖子数+排除类别
【发布时间】:2012-01-04 18:12:22
【问题描述】:

在我的主页上,我使用 query_posts('posts_per_page=2') 限制了最近显示的帖子数量

我还想排除一个类别,但不知道如何将这两个修改集成到循环中。

这是我的原始代码:

<?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();?>

我尝试了几个不同的教程,但无法让它们发挥作用。我确定这是我自己的理解不足,因此任何明确的说明或对上述代码的修改将不胜感激。

提前致谢!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在您的查询中尝试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();
    ?>
    

    【讨论】:

    • 其中 # 是您的类别 ID
    • @Matt H 就是这样!只是不确定如何将两者结合起来。非常感谢!
    • 嗯......它确实有效,但现在我发现我所有的帖子都以相反的顺序显示,这意味着最旧的帖子排在第一位,而不是最近的帖子。当我删除 cat=-# 语法时,帖子会再次正确显示。有趣的是,这个问题只出现在远程服务器上。在我的本地开发环境中,所有帖子的顺序都正确。想法?
    • 您可以强制订购,例如查询中的&amp;order=ASC&amp;order=DESC。我不知道为什么会有所不同 - 可能是管理面板中的常规设置。
    • &amp;order=ASC 确实颠倒了顺序,但奇怪的是,仅当不包括 cat=-# 语法时才如此。当cat=-# 语法到位时,它就不再起作用了。诡异的!我在管理面板中搜索了一个设置,但没有看到。也在网上搜索,但没有运气。还有其他建议吗?再次感谢!
    【解决方案2】:

    请试试这个:

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

    【讨论】:

    • OP 要求排除类别,在这种情况下,您必须将类别设为 -3​​。
    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2013-02-01
    • 2013-10-29
    • 1970-01-01
    • 2021-08-07
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多