0
votes

I'm having some real trouble with the below code in my taxonomy.php template in Wordpress. The query is working (i.e pulling posts only from within that custom taxonomy) but it is only displaying 2 posts (4 are in the taxonomy).

All my efforts to convert this into a standard loop using $args simply result in posts from all taxonomies being pulled into the page. I was hoping it is as simple as adding posts_per_page => -1 but this just causes every post in the entire site to display.

As I understand it from the codex, taxonomy pages should pull the relevent posts by default, without a need for a loop?

Any help much appreciated!

taxonomy.php

<?php get_header(); ?>

<main>    
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
        <figure>
            <?php if ( has_post_thumbnail() ) { 
                  the_post_thumbnail();
            } ?>
            <figcaption>
                <h4><?php the_title(); ?></h4>
                <h5><?php the_excerpt(); ?></h5>
            </figcaption>
        </figure>
<?php endwhile; ?>
<?php endif; ?>   
</main>

<?php get_footer(); ?>

UPDATE

<main> 

<?php
$args = array(
    'posts_per_page' => -1
);

$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <figure>
            <?php if ( has_post_thumbnail() ) { 
                  the_post_thumbnail();
            } ?>
            <figcaption>
                <h4><?php the_title(); ?></h4>
                <h5><?php the_excerpt(); ?></h5>
            </figcaption>
        </figure>
<?php endwhile; ?>
<?php endif; ?>   
</main>

<?php get_footer(); ?>
1
Will you provide your custom taxonomy name, so that I can provide you code to show proper posts relevant to that taxonomy ? - laraib
Hi @laraib, I actually have 6 Taxonomies, each with multiple 'Terms'. The taxonomy page is used when a user clicks through from an Index page by clicking on a 'Term' - They should then be shown x number of posts form within that 'Term'. Make sense? My 6 taxonomies are: 'topics', 'places', 'dates', 'interviewee', 'period' and 'a-z'. Thanks - dungey_140

1 Answers

2
votes

If you have 6 different taxonomies then there will be 6 different template files to show those taxonomies appropriately. In your case your templates will be taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.php and taxonomy-a-z.php

In this way once these templates are created your templates will be showing appropriate posts. To achieve that you can use posts_per_page argument or you can visit this page for better understanding about fetching posts WP_Query Codex Page Hope that makes sense by now