自定义帖子类型Wordpress按类别查询 [英] Custom post type Wordpress query by category
问题描述
我有以下查询,该查询为我的自定义帖子类型输出名为STORIES的类别列表.
I have the following query which outputs a list of categories for my custom post type called STORIES.
<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div> ';
}
?>
这会输出我的类别的链接列表,效果很好.
This outputs a list of links for my categories and works great.
我的问题是,我不知道如何在下一页上写查询,该查询将列出所选类别中的所有帖子.
My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.
因此我的查询列出了类别...- 苹果-橘子-香蕉
So my query lists the categories... - Apples - Oranges - Bananas
如果您单击Apples并转到该页面,我将使用什么查询来列出所有具有APPLES类别的故事?
If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?
有什么想法吗?无法获得任何解决方案.
Any ideas? Can't get any solution to work.
我有以下查询,但它列出了所有类别以及其中的所有帖子.如何修改它以仅显示我所在页面的帖子?
I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?
<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
endwhile;
}
}
?>
推荐答案
you can create custom taxonomy template for custom post : LINK
这篇关于自定义帖子类型Wordpress按类别查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!