自定义帖子类型分类页面仍显示所有帖子

时间:2017-07-03 14:39:58

标签: php wordpress

我有自定义帖子类型的子导航'项目'分类学称为"类型":

        <?php $args = array( 'post_type' => 'projects');
        $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php   $terms = get_terms('type');
            $currentterm = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
                echo '<ul class="sub-nav-menu">';
                foreach ($terms as $term) {
                    $class = $currentterm->slug == $term->slug ? 'live' : '' ;
                    echo '<li><a href="'.get_term_link($term).'" class="'. $class .'">'.$term->name.'</a></li>';
                    }
                echo '</ul>'; ?>
            <?php endwhile; ?>
            <?php endif; ?>

点击分类法时,会转到分类法页面taxonomy-type.php。

虽然此页面仍然显示所有自定义帖子类型,而不仅仅是当前分类页面的类型。

<?php $args = array( 'post_type' => 'projects');
                    $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>
            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php   $terms = get_terms('type'); ?>
            <a href="<?php the_permalink() ?>">
                <h3><?php the_title(); ?></h3>
            </a>
    <?php wp_reset_postdata(); ?>
    <?php endwhile; ?>
<?php endif; ?>

我应该如何修改循环以仅过滤&#39;类型&#39;的当前分类标准帖子。分类

1 个答案:

答案 0 :(得分:1)

您可以按分类法中的分类法获取帖子列表 - {post_type} .php在此文件中,默认情况下为指定分类法的帖子列表。

在当前活动主题文件夹中创建此文件,并使用下面的代码

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if
?>
相关问题