自定义 post 按类别键入 Wordpress 查询

Custom post type Wordpress query by category

我有以下查询,它为我的名为 STORIES 的自定义 post 类型输出类别列表。

<?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>  ';
}
?>

这会输出我的类别的链接列表,效果很好。

我的问题是,我不知道如何在下一页上编写查询,该页面将列出所选类别中的所有 post。

所以我的查询列出了类别... - 苹果 - 橘子 - 香蕉

如果您单击苹果并转到该页面,我使用什么查询来列出类别为苹果的所有故事?

有什么想法吗?无法获得任何解决方案。

我有以下查询,但它列出了所有类别和其中的所有 post。我如何修改它以仅显示我所在页面的 post?

<?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;
}
}
?>

希望对您有所帮助:

 $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
           'taxonomy' => 'story-category',
           'field'    => 'slug',
           'terms'    => $term->slug,
        ),
    ),
);
$query = new WP_Query( $args );

Class Reference/WP Query

您可以为自定义 post 创建自定义分类模板:LINK