添加& Taxonomy.php/分类页面中单独的自定义帖子类型
我有一个taxonomy.php 文件来显示分类术语。我在functions.php 中添加了一个过滤器,以包含分类页面查询的帖子类型。此过滤器:
add_filter( 'pre_get_posts' , 'ucc_include_custom_post_types' );
function ucc_include_custom_post_types( $query ) {
global $wp_query;
/* Don't break admin or preview pages. */
if ( !is_preview() && !is_admin() && !is_page() && !is_single() ) {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
if ($query->is_feed) {
// Do feed processing here.
} else {
$my_post_type = get_query_var( 'post_type' );
if ( empty( $my_post_type ) )
$query->set( 'post_type' , $post_types );
}
}
return $query;
}
返回您想要的任何和所有帖子类型。但我正在努力寻找一种方法将它们分开。我尝试使用正常的循环,但我不知道如何从页面获取当前的分类标记。
我有两个相关的问题,但想看看解决这个问题的最佳方法是什么。假设我有 3 个帖子类型('post' 'post2' 'post3')
- 是否有一个循环可以在taxonomy.php 中使用来显示特定的帖子类型?那么每种帖子类型可以有一个循环吗?因此,当我单击分类术语时,taxonomy.php 将返回:
--Taxonomy Page --
Loop for custom type post 1 (show post with currenttaxonomy tag in this certain post type)
Loop for custom type post 2
Loop for custom type post 3
- 如果有多个循环,会影响分页吗?或者分页仅适用于帖子?
我在taxonomy.php页面中使用了许多单循环但无济于事。我觉得我必须将当前的分类术语变量回显为新变量:
$term = $wp_taxonomies??
有什么办法可以在taxonomy.php页面中进行多个循环吗?
I have a taxonomy.php file to display taxonomy terms. I added a filter in functions.php to include post types for the taxonomy page query. This filter:
add_filter( 'pre_get_posts' , 'ucc_include_custom_post_types' );
function ucc_include_custom_post_types( $query ) {
global $wp_query;
/* Don't break admin or preview pages. */
if ( !is_preview() && !is_admin() && !is_page() && !is_single() ) {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ) );
if ($query->is_feed) {
// Do feed processing here.
} else {
$my_post_type = get_query_var( 'post_type' );
if ( empty( $my_post_type ) )
$query->set( 'post_type' , $post_types );
}
}
return $query;
}
Returns any and all post types you want. But I am trying to find a way to separate them. I tried to use a normal loop but I don't know how to fetch the current taxonomy tag from the page.
I have 2 questions which are all related but seeing what is the best way to go about this. Pretend I have 3 posts types ('post' 'post2' 'post3')
- Is there a loop that can be used in taxonomy.php that will display a particular post type? So it can be possible to have one loop for each post type? So when I click on a taxonomy term, the taxonomy.php will return:
--Taxonomy Page --
Loop for custom type post 1 (show post with current taxonomy tag in this specific post type)
Loop for custom type post 2
Loop for custom type post 3
- If there are multiple loops, will this affect the pagination? Or will pagination only work for posts?
I have used many single loops in the taxonomy.php page to no avail. I feel I have to echo the current taxonomy term variable to a new variable:
$term = $wp_taxonomies??
Any way for multiple loops in the taxonomy.php pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法可能是忽略现有的
$wp_query
并在分类模板中创建三个新查询。因此,不要在模板中启动“循环”,只需创建一个新查询并使用该查询循环即可。对其他帖子类型重复此操作。这也意味着您不需要连接到pre_get_posts
过滤器,您可以为您创建一个自定义查询。事实上,您必须考虑下一页的用户界面。这取决于您希望将帖子类型分开的原因。如果您在第一页上看到这三个链接就足够了,您可以使用三个单独的“下一页”链接,因此每个帖子类型都有一个链接。
Probably the easiest way to do this is ignore the existing
$wp_query
and create three new queries in your taxonomy template. So don't start "The Loop" in your template, just create a new query and loop with that one. Repeat this for the other post types. This also means you don't need to hook into thepre_get_posts
filter, you create a custom query just for you.You will have to think about the UI for the next pages, indeed. This depends on the reason you want the post types separated. If it is enough that you see the three together on the first page, you could go with three separate "next page" links, so one per post type.