【问题标题】:WordPress - How to get posts in a category but exclude those under sub categories?WordPress - 如何获取某个类别中的帖子但排除子类别下的帖子?
【发布时间】:2010-05-08 03:44:58
【问题描述】:

假设类别是A,它有一个子类别subA,其中包括一个帖子postinsubA

然后当我使用get_posts('category=A&...') 时,ApostinsubA 类别下的所有帖子都被返回,但我不想postinsubA,我如何在子类别中排除这些帖子?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    查看 Wordpress 手册,其中的 query_posts() 函数确实有一个可能适合您的参数。

    这是一个示例,仅从类别 129 中提取帖子,但不从 129 的任何子类别中提取帖子:

    query_posts(array('category__in' => array(129)));
    while(have_posts()) { the_post();
       echo '<li>'.the_title().'-'.the_category().'</li>';
    }
    

    您还可以向其中添加更多类别,例如 array(128,129)。我在我自己的一个 Wordpress 博客上做了一个快速测试,其中父母 (129) 有 2 个帖子,孩子 (139) 有 1 个帖子。在打印循环时,仅显示类别 129 中的 2 个帖子。

    【讨论】:

    • 另外,附带说明...如果您使用的是 Wordpress 2.6+,则可以在 get_posts() 中使用相同的参数,因为它们使用相同的数据库查询类。比如:get_posts(array('category__in' => array(129))); * 未测试 *
    猜你喜欢
    • 2011-11-07
    • 2020-04-18
    • 2013-10-29
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多