首页 文章

如何显示自定义分类的特定术语的帖子?

提问于
浏览
0

我为自定义帖子创建了自定义分类 . 现在我想显示与特定术语相关的所有帖子 . 假设我有两个术语term1和term2 . 点击term1后,所有与term1相关的帖子都会显示并发布,与term2相关的帖子将不会显示 . 但是现在当我点击term1时,就会一次显示与term1和term2相关的帖子 . 我已将以下代码写入 taxonomy.php 模板 .

<div class="main-content">
                <?php
                $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

                    $the_query = new WP_Query( array(
                        'post_type' => array('newsbox_post'),
                        'tax_query' => array(
                            'taxonomy' => $term->taxonomy,
                            'field' => 'slug',
                            'terms' => $term->name,
                        ),
                    ) );

                ?>
                <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
                    <div class="post">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                            <?php the_content(); ?> 
                            <?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' );  ?>
                            <hr />
                    </div>

                <?php 
                    endwhile;
                    wp_reset_postdata();
                    else:?>
                        <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
                <?php
                    endif;
                ?>  
            </div>

请告诉我,我怎样才能做到这一点 .

2 回答

  • 0

    我有答案 .

    $the_query = new WP_Query( array(
                            'post_type' => array('newsbox_post'),
                            'tax_query' => array(
                                array(
                                    'taxonomy' => $term->taxonomy,
                                    'field' => 'slug',
                                    'terms' => $term->slug,
                                ),
                            ),  
                            ) );
    
  • 0

    您应该使用 slug 而不是 name ,如下面的代码: -

    $the_query = new WP_Query( array(
                        'post_type' => array('newsbox_post'),
                        'tax_query' => array(
                           array(
                                'taxonomy' => $term->taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                           ),
                        ),
                    ) );
    

    希望这会帮助你 .

相关问题