Wordpress get_posts按类别
-
-
它不是分类名称而不是类别吗?Shouldn't it be taxonomy name instead of category?
- 0
- 2015-06-30
- Robert hue
-
我尝试过,但是没有用.我是从wordpress的法典页面上得到的,这似乎支持我的方法,但是仍然行不通:"注意:category参数必须是类别的ID,而不是类别名称."I tried it, but it didn't work. I got this off the codex-page of wordpress, which seems to support my approach, but still, it doesn't work: "Note: The category parameter needs to be the ID of the category, and not the category name."
- 0
- 2015-06-30
- Michiel Standaert
-
posts_per_page中的减号1(-1)将显示所有帖子,而您一旦离开CPT,wp将在您已经发现自己的常规帖子中"回退".The minus1 (-1) in posts_per_page will show ALL posts and as soon you leave out the CPT wp will "fall back" at the regular posts as you already found out yourself.
- 0
- 2015-07-01
- Charles
-
2 个回答
- 投票数
-
- 2015-07-01
很可能您使用的是自定义分类法,而不是内置的
category
分类法.在这种情况下,类别参数将不起作用.您将需要tax_query
来查询特定术语的帖子. (请记住,get_posts
使用WP_Query
,因此您可以将WP_Query
中的任何参数传递给get_posts
)$args = [ 'post_type' => 'product', 'tax_query' => [ [ 'taxonomy' => 'my_custom_taxonomy', 'terms' => 7, 'include_children' => false // Remove if you need posts from term 7 child terms ], ], // Rest of your arguments ];
其他资源
In all probability you are using a custom taxonomy, and not the build-in
category
taxonomy. If this is the case, then the category parameters won't work. You will need atax_query
to query posts from a specific term. (Remember,get_posts
usesWP_Query
, so you can pass any parameter fromWP_Query
toget_posts
)$args = [ 'post_type' => 'product', 'tax_query' => [ [ 'taxonomy' => 'my_custom_taxonomy', 'terms' => 7, 'include_children' => false // Remove if you need posts from term 7 child terms ], ], // Rest of your arguments ];
ADDITIONAL RESOURCES
-
- 2015-07-01
<ul> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; wp_reset_postdata();?> </ul>
可以这会为您提供帮助.
谢谢
<ul> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; wp_reset_postdata();?> </ul>
May this will help You.
Thanks
-
一些解释会很好.Some explanation will be great.
- 3
- 2015-07-01
- Nilambar Sharma
-
现在,他将从哪个"post_type"获得链接** s **,在您的情况下为5?恕我直言,他希望*产品*的*内容*(据我所知是CPT),而不是常规文章中的任何内容.From which 'post_type' he will get now the link**s**, 5 in your case? Imho he wants the *content* of *products*(as I understand a CPT) and nothing from the regular posts.
- 0
- 2015-07-01
- Charles
-
在参数中传递您的类别ID,然后从常规帖子中获得5个帖子.pass your category id in arguments and from regular post you will get 5 posts.
- 0
- 2015-07-01
- Rohit gilbile
-
请阅读他的问题,我并不总是对的,但是在这种情况下,他希望从Custom Post Type中以" Product"为名的"东西".Read his question please, I am not always right but in this case he wants 'something' from a Custom Post Type with the name Product.
- 0
- 2015-07-01
- Charles
-
查尔斯在这种情况下是对的.我知道发布帖子后如何获取数据.问题是我没有收到我的自定义帖子:)Charles is right in this case. I know how to get the data once I have my posts. The problem was that I wasn't getting the my custom posts :)
- 0
- 2015-07-01
- Michiel Standaert
-
@Rohitgilbile如何在foreach循环中包含特色图像?@Rohitgilbile how to include featured image inside foreach loop ?
- 0
- 2018-05-09
- user2584538
我有以下代码:
这应该返回我知道属于该类别的帖子,但不是.如果我不考虑"类别"参数,那么我将获得所有产品,因此我知道这通常是可行的.如果将类别更改为1并删除自定义帖子类型(产品),则会获得默认帖子.
我看不出这是怎么回事.谁能找到问题所在?