[英]Custom Post Type displays post belong on the category
I have a custom post type advice
and taxonomy adcat
. 我有一个自定义帖子类型
advice
和分类法adcat
。 I want to display all post belong to that category. 我想显示属于该类别的所有帖子。
Lets say I have 4 categories namely : 'games', 'tours', 'dishes', 'hotels' and also this four category is a menu. 可以说我有4个类别,即“游戏”,“旅游”,“菜肴”,“酒店”,这4个类别也是菜单。 If I click one of the category for example: hotels all post belong to the hotels should display.
例如,如果我单击类别之一:所有显示酒店的帖子都应显示。
By the way this code I used to show wordpress default categories: 顺便说一下,我曾经用这段代码来显示wordpress默认类别:
<?php $catname = wp_title('', false); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=8&offset=0");
foreach ($posts as $post) : start_wp(); ?>
//html output
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
this is not working in custom post 'taxonomies' any suggestion would be helpful thank's 这不适用于自定义帖子“分类法”,任何建议都会有所帮助,谢谢
try something like this, this is the example to fetch post by category id 尝试这样的事情,这是按类别ID获取帖子的示例
$args = array( 'cat' => $cat_id, 'post_type' => 'advice', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
endwhile;
wp_reset_postdata();
you can also use with category name as well. 您也可以使用类别名称。
$args = array('category_name' => 'catname', 'post_type' => 'advice', 'posts_per_page' => -1 );
you can use post_per_page as per your requirement, I have just added -1 for all the posts 您可以根据需要使用post_per_page,我为所有帖子添加了-1
I don't really understand your terminology. 我不太了解您的用语。 When you are talking about
adcat
, is it a custom taxonomy or a term of the build-in taxonomy category
. 在谈论
adcat
,它是自定义分类法还是内置分类法category
的术语。 If adcat
is a custom taxonomy, you should use the tax_query
in WP_Query
,not the category parameters. 如果
adcat
是自定义分类法,则应在tax_query
中使用WP_Query
,而不是类别参数。
Remember, category and a custom taxonomy are both taxonomies, their direct children is called terms, and their direct childen is called child terms 请记住,类别和自定义分类法都是分类法,其直接子级称为术语,而其直接子级称为子术语
You should also not be using wp_title()
to get the queries object. 您也不应使用
wp_title()
获取查询对象。 You should be using get_query_var()
to get the queried object. 您应该使用
get_query_var()
来获取查询的对象。 For categories it will be cat
, taxonomy will be taxonomy
and for terms term
. 对于类别将是
cat
,分类将taxonomy
和术语term
。 Have a look at get_categories
, get_taxonomies
and get_terms
for returned values 看看
get_categories
, get_taxonomies
和get_terms
的返回值
Example 例
$category = get_query_var( `cat` );
$cat_slug = $category->slug;
You can now return $cat_slug
to your custom query as category_name
现在,您可以将
$cat_slug
作为category_name
返回到自定义查询
EDIT 编辑
I've quickly rethinked the whole thing and checked your comment as well. 我很快重新考虑了整个过程,并检查了您的评论。 Why not just copy your index.php and rename it taxonomy.php.
为什么不直接复制index.php并将其重命名为taxonomy.php。 There is no need at all for a custom query here.
此处完全不需要自定义查询。 The default loop in taxonomy.php should do it
taxonomy.php中的默认循环应该做到这一点
EDIT 2 编辑2
For further reading, go and check the following articles 要进一步阅读,请查看以下文章
Not sure if I read your answer correctly but what I am assuming is you want to search posts by the taxonomy terms correct? 不确定我是否正确阅读了您的答案,但是我假设您是要按照正确的分类学术语搜索帖子? So in your
adcat
taxonomy you have 'games', 'tours', 'dishes', 'hotels' as your terms or categories. 因此,在
adcat
分类中,您将“游戏”,“旅游”,“菜”,“酒店”作为您的术语或类别。 Your best option would be to use a tax_query
in your query arguments. 最好的选择是在查询参数中使用
tax_query
。 Here is how to do that with the WP_Query() object. 这是使用WP_Query()对象执行此操作的方法。
<?php
$args = array(
'post_type' => 'advice',
'posts_per_page' => 8,
'tax_query' => array(
'taxonomy' => 'adcat',
'terms' => array('games', 'tours', 'dishes', 'hotels'),
'field' => 'slug'
)
);
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Important Note: When using a WP_Query you will be overwriting the default post data. 重要说明:使用WP_Query时,您将覆盖默认的帖子数据。 So in order to get that data back you just use the
wp_reset_postdata()
as you can see in the example above after the loop has finished. 因此,为了取回该数据,您只需使用
wp_reset_postdata()
如循环结束后在上例中所见。
try this maybe it work... I write a note so you can see whats going on.. hope it help 试试这个也许行得通...我写了一条便条,这样您就可以看到发生了什么..希望对您有所帮助
<?php
// Get the term/category of the post
$terms = get_the_terms( $post->ID , 'advice-cat' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'advice cat' );
}
//WordPress loop for custom post type
$terms = get_the_terms( $post->ID , 'advice-cat' );
$my_query = new WP_Query('post_type=advice&advice-cat=' . $term->name . '&posts_per_page=-1');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
// output content
<?php the_title(); ?>
<?php endwhile; wp_reset_query(); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.