Custom Post Type taxonomy page still showing all p

2019-08-18 18:14发布

问题:

I have a sub-navigation for custom post type 'Projects' taxonomy called "Type":

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

When clicking on taxonomy it takes you to the taxonomy page taxonomy-type.php.

Although this page is still showing all the custom post types not just the ones for the current taxonomy page.

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

How should I modify the loop to filter only the current taxonomy posts of the 'Type' taxonomy?

回答1:

you can get post list by taxonomy in taxonomy-{post_type}.php In this file by default gt list of post for specified taxonomy.

Create like this file in current active theme folder and use below code,

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


标签: php wordpress