0

I have created a custom post type "brochures" which i am currently displaying with the code below (using ACF to display the brochure file).
At the moment this displays all the brochures individually.

I have set up a custom taxonomy for this post type called "brochure_categories" so that i can categories the brochures into separate folders like the below.

Brochure Category 1
Brochure 1
Brochure 2

Brochure Category 2
Brochure 3
Brochure 4

Brochure Category 3
Brochure 5
Brochure 6

Rather displaying all the posts (like im doing with the code below), i want to display all of the brochure categories, and when a category is clicked it then displays the posts within that category.

How am i able to achieve this?

<ul class="brochures">
    <?php 
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=18&orderby=title&order=ASC&post_type=brochures'.'&paged='.$paged); 

  while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

  <li><a target="_blank" href="<?php the_field('brochure_file'); ?>"><img src="<?php the_field('brochure_logo'); ?>"><h3><?php echo get_the_title(); ?></h3><h4>Click to view</h4></a>
            </li>

<?php endwhile; ?>

<nav class="nextPrev">
    <div class="prev"><?php previous_posts_link('&laquo; Previous') ?></div>
    <div class="next"><?php next_posts_link('Next &raquo;') ?></div>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

</ul>
  • If you've set up a custom taxonomy, why not just use the native term archives, and the taxonomy-{taxonomy-name}.php template - no need for custom querying here. – TheDeadMedic May 26 '16 at 15:18
0

To retrieve all the terms attached to your taxonomy "brochure_categories", you can use get_terms() function. More info here.

Once you've got your terms, you can traverse them and query your custom posts with:

$query = new WP_Query( array(
  'post_type'             => 'brochures',
  'brochure_categories'   => $current_category,
  'posts_per_page'        => -1,
  'order'                 => 'ASC',
  'orderby'               => 'title',
) );

You can now display them, by category, as it suits you.

Is it ok for you ?

0
try This Code Change taxonomy name which you are using 

/**post type name: brochures **/
/** taxonomy name: brochures-cat **/
<?php 
  $custom_terms = get_terms('brochures-cat');
  foreach($custom_terms as $custom_term) {
   wp_reset_query();
   $args = array(
      'post_type' => 'brochures',
      'tax_query' => array(
          array(
           'taxonomy' => 'brochures-cat',
           'field' => 'slug',
           'terms' => $custom_term->slug,
          ),
         ),
      );
   $loop = new WP_Query($args);
   if($loop->have_posts()) {
    echo '<h2>'.$custom_term->name.'</h2>';
    while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br />';
    endwhile;
   }
  }
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.