【问题标题】:Get posts from multiple categories with category使用类别从多个类别中获取帖子
【发布时间】:2015-03-17 14:38:34
【问题描述】:

我想要获取具有特定类别的特定类别中的一个(不是全部)的帖子。

例如,我想要所有类别为 Apples、Bananas、Oranges 且类别为 GreenBeans 的帖子。

如何在 Wordpress 中做到这一点?

【问题讨论】:

  • 是的,你可以这样做。您可以指定要拉入循环的类别。阅读 wordpress 文档。

标签: php wordpress


【解决方案1】:

我认为您应该查看 Wp_query 的 codex

下面的示例将返回类别名称为“staff”和“news”的 allpost:

$query = new WP_Query( 'category_name=staff+news' );

查看此页面了解更多信息: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

【讨论】:

    【解决方案2】:

    我想我明白了。

    $args= array(
        'post_type' => 'post',
        'posts_per_page' => '1',
        'orderby' => 'rand',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'category-1',
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'category-2',
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'category-3',
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => 'category-4',
                ),
            ),
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'category-5',
            ),
        )
    );
    

    【讨论】:

      【解决方案3】:

      当然可以!这就是像 WordPress 这样的 CMS 的全部意义所在。开始学习吧……这里是函数get_terms()的文档。

      还有其他一切WordPress Documentation

      祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-28
        • 2011-08-29
        • 2011-05-16
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多