admin管理员组文章数量:1313187
I've seen this question posted in different forms before, but most of those solutions are centered around the admin interface, and I haven't found any answers that apply to the front end.
I've got a custom post type, and an associated custom taxonomy. I'm using the archive page (archive-{custom_type}.php
) to display the items, and using wp_list_categories
to show a list of the custom taxonomy terms. I can manually alter the posts displayed by adding a tax_query
parameter to the WP_Query
call, but the problem I'm running into is I cannot figure out how to alter the taxonomy links so they point to this archive page so I can filter dynamically. I'd rather not duplicate this template's markup and code in a taxonomy-{custom_type}.php
file.
Do I need to just output the taxonomy links manually? How should the URL be structured so I can get the query param? I have query_var => true
set and a rewrite rule on the custom taxonomy definition, but haven't been able to get get_query_var()
to return anything.
The end result should be a template that can list all items in the custom post type, or filter those items by their associated custom taxonomy.
I've seen this question posted in different forms before, but most of those solutions are centered around the admin interface, and I haven't found any answers that apply to the front end.
I've got a custom post type, and an associated custom taxonomy. I'm using the archive page (archive-{custom_type}.php
) to display the items, and using wp_list_categories
to show a list of the custom taxonomy terms. I can manually alter the posts displayed by adding a tax_query
parameter to the WP_Query
call, but the problem I'm running into is I cannot figure out how to alter the taxonomy links so they point to this archive page so I can filter dynamically. I'd rather not duplicate this template's markup and code in a taxonomy-{custom_type}.php
file.
Do I need to just output the taxonomy links manually? How should the URL be structured so I can get the query param? I have query_var => true
set and a rewrite rule on the custom taxonomy definition, but haven't been able to get get_query_var()
to return anything.
The end result should be a template that can list all items in the custom post type, or filter those items by their associated custom taxonomy.
Share Improve this question asked May 17, 2016 at 15:12 ken.dunningtonken.dunnington 2032 silver badges8 bronze badges 1- 2 I frequently need this on sites and I always find it neater to duplicate the template. To prevent duplicate code though, you can use get_template_part() to pull a common piece of PHP into both templates, or even to pull one template into the other. – Andy Macaulay-Brook Commented May 19, 2016 at 14:52
2 Answers
Reset to default 1Let say you have "book" post type and "genre" taxonomy. And you want to get books with genre of "scifi".
You can pass the parameter in the url using:
?taxonomy=genre&term=scifi
Then you can get those parameter using get_query_var('taxonomy')
and get_query_var('term')
and add them to the WP_Query arguments.
$taxonomy = get_query_var('taxonomy');
$term = get_query_var('term');
$args = array(
'post_type' => 'book',
'tax_query' => array(
array(
'field' => 'slug',
'taxonomy' => $taxonomy,
'terms' => $term,
)
),
);
$query = new WP_Query($args);
First I have taxonomy "service".
Here I make a different php file, for the taxonomy "service" loop and the default WordPress loop (post).
which will be called in archive.php
it will look the same
First I will create a file for the "service" taxonomy named my-loop-service.php
Fill in the code:
<div class="my-service">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class('my-service-loop'); ?>>
<h1 class="my-service-title"><?php the_title(); ?></h2>
</div>
<?php endwhile; endif; ?>
</div>
Secondly I make a file for the default post loop with the name my-loop-post.php
Fill in the code:
<div class="my-post">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class('my-post-loop'); ?>>
<h1 class="my-post-title"><?php the_title(); ?></h2>
</div>
<?php endwhile; endif; ?>
</div>
And the following is the contents of the archive.php file
<?php
get_header();
if (is_post_type_archive('service') || is_tax('services-category') || is_tax('services-tags')){
get_template_part('my-loop-service'); // Get Loop for Taxonomy service
} else {
get_template_part('my-loop-post'); // Default Loop for Post
}
// Navigation
echo '<div id="my-navigation">';
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Prev', 'text-domain' ),
'next_text' => __( 'Next', 'text-domain' ),
));
echo '</div>';
get_footer();
?>
That way there is no need to create a special php archive file for your taxonomy.
本文标签: Filtering a custom post type by custom taxonomy in archive template
版权声明:本文标题:Filtering a custom post type by custom taxonomy in archive template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741921769a2405044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论