Loop posts based on permalink term

I have setup my custom post’s custom taxonomy’s permalink structure to be:

custom post type > custom taxonomy > post (from this helpful Q&A)

Now I’m hoping to be able to code taxonomy.php to automatically loop posts based on the current permalink, ie: if the URL is example.com/writing/fiction/ then the page (taxonomy.php) will show all posts with the taxonomy term “fiction” or if the URL is example.com/writing/non-fiction then taxonomy.php will show all posts with taxonomy term “non-fiction”.

The goal is to only need 1 taxonomy.php file.

Here is current code (comment where I’m looking to make term automatic/dynamic):

<?php
$posts = array (
‘post_type’ => ‘writing’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘type’,
‘terms’ => ‘fiction’, // looking to automate this, based on URL
),
),
);
$loop = new WP_Query( $posts );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<a href=”<?php the_permalink();?>”>
<?php the_title(‘<h2>’,'</h2>’);?>
</a>
<?php endwhile; wp_reset_postdata(); ?>

Read more here:: Loop posts based on permalink term

Leave a Reply

Your email address will not be published. Required fields are marked *