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

How to show post for a particular term of custom taxonomy?

我为自定义帖子创建了自定义分类法。现在,我想显示与特定术语相关的所有帖子。假设我有两个词term1和term2。单击term1时,将显示与term1相关的所有帖子,而与term2相关的post将不显示。但是现在当我单击term1时,一次显示与term1和term2相关的帖子。我已将以下代码写入taxonomy.php模板。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
                <?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(); ?>
                   
                           " title="<?php the_title_attribute(); ?>"><?php the_title(); ?>
                            <?php the_content(); ?>
                            <?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' );  ?>
                            <hr />
                   

                <?php
                    endwhile;
                    wp_reset_postdata();
                    else:?>
                        <?php _e('404 Error Not Found'); ?>
                <?php
                    endif;
                ?>

请告诉我,我该如何实现。


我知道了。

1
2
3
4
5
6
7
8
9
10
                    $the_query = new WP_Query( array(
                        'post_type' => array('newsbox_post'),
                        'tax_query' => array(
                            array(
                                'taxonomy' => $term->taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                            ),
                        ),  
                        ) );

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

1
2
3
4
5
6
7
8
9
10
$the_query = new WP_Query( array(
                    'post_type' => array('newsbox_post'),
                    'tax_query' => array(
                       array(
                            'taxonomy' => $term->taxonomy,
                            'field' => 'slug',
                            'terms' => $term->slug,
                       ),
                    ),
                ) );

希望这会对您有所帮助。