【发布时间】:2015-03-17 14:38:34
【问题描述】:
我想要获取具有特定类别的特定类别中的一个(不是全部)的帖子。
例如,我想要所有类别为 Apples、Bananas、Oranges 且类别为 GreenBeans 的帖子。
如何在 Wordpress 中做到这一点?
【问题讨论】:
-
是的,你可以这样做。您可以指定要拉入循环的类别。阅读 wordpress 文档。
我想要获取具有特定类别的特定类别中的一个(不是全部)的帖子。
例如,我想要所有类别为 Apples、Bananas、Oranges 且类别为 GreenBeans 的帖子。
如何在 Wordpress 中做到这一点?
【问题讨论】:
我认为您应该查看 Wp_query 的 codex
下面的示例将返回类别名称为“staff”和“news”的 allpost:
$query = new WP_Query( 'category_name=staff+news' );
查看此页面了解更多信息: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
【讨论】:
我想我明白了。
$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',
),
)
);
【讨论】:
当然可以!这就是像 WordPress 这样的 CMS 的全部意义所在。开始学习吧……这里是函数get_terms()的文档。
还有其他一切WordPress Documentation。
祝你好运。
【讨论】: