繁体   English   中英

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

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

我为自定义帖子创建了自定义分类法。 现在,我想显示与特定术语相关的所有帖子。 假设我有两个词term1和term2。 单击term1时,将显示与term1相关的所有帖子,而与term2相关的post将不显示。 但是现在当我单击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>

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

您应该使用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,
                       ),
                    ),
                ) );

希望这会帮助你。

我有答案。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM