简体   繁体   English

WordPress按类别获取自定义帖子类型的帖子

[英]Wordpress get custom post type posts by category

Hi I have some problem with custom post type categories, I try to get all post that have some category but it output all post. 嗨,我对自定义帖子类型类别有一些问题,我尝试获取具有某个类别的所有帖子,但它输出所有帖子。 Can you help me please. 你能帮我吗。 Thank you. 谢谢。

my code 我的代码

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => "collection-posts",
    'category'=> 1,
    'posts_per_page' => 12,
    'paged' => $paged
);
query_posts($args);

but it give all posts. 但它给出了所有帖子。 I don't understand why 我不明白为什么

The arguments key should be cat , not category : arguments键应该是cat ,而不是category

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => "collection-posts",
    'cat' => 1,
    'posts_per_page' => 12,
    'paged' => $paged
);
query_posts($args);

I found another solutions. 我找到了另一个解决方案。 You can also use get get_posts method of WordPress and category slug. 您还可以使用WordPress的get get_posts方法和类别标签。 For Example : i am assuming your category slug is collection-posts-category 例如:我假设您的类别子弹是collection-posts-category

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
     'category'  => 'collection-posts-category',
     'post_type' => 'collection-posts',
     'posts_per_page' => 12,
     'paged' => $paged
);
$collection_posts = get_posts($args);

Hope this will helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM