Add & Separate Custom Post Types in Taxonomy.php / Taxonomy Page
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 k开发者_如何转开发now 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?
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 the pre_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.
精彩评论