0

I have a simple issue that can’t solve.

I have a Custom Post Type called “Hardware” and inside a taxonomy called “hardware_category”. I'm trying to display a list of my custom posts in each term with the file taxonomy-hardware_category.php but doesn’t return me any result (it should display 5 posts).

Anything special to consider in my template?

Thanks!

<?php 

// File: taxonomy-hardware_category.php

get_header(); 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>

<div class="main">

    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>


        <div class="product">
            <?php the_post_thumbnail('full'); ?>
            <?php the_title(); ?>
        </div>

        <?php endwhile; ?>
    <?php endif; ?>

</div>

<?php get_footer(); ?>
2
  • Are you trying to display a list of posts in each term, or a list of terms themselves? – Jacob Peattie Feb 15 '20 at 2:00
  • Trying to display a list of posts in each term, thanks for answer. – Sema Hernández Feb 15 '20 at 2:23
0

Well, first of all, you are not doing anything with the $term variable. You are querying it, but then it sits there, waiting to be picked up.

Try this code in your taxonomy template, called taxonomy-hardware_category.php:

// prepare the variables for the query
$tax = get_query_var('taxonomy');
$term = get_query_var('term');

// Setup the loop, build the html, return it, clean up the query
    $hardware_args = array(
        'post_type' => 'hardware',
         // This should get you the result
         // the two arrays isn't a mistake
         'tax_query' => array(
                        array(
                            'taxonomy' => $tax,
                            'field' => 'slug',
                            'terms' => $term
                        )
                    ),
                    'posts_per_page' => '30',
                    'orderby' => 'name',
                    'order'=>'ASC'
                );
        $loop = new WP_Query($hardware_args );
        $html = '';
        while ($loop->have_posts()) : $loop->the_post();

        $html .= '<div class="some-custom-class-like col-md-12">';
            $html .= '<div class="hardware">';
                $html .= '<a href="'.get_the_permalink().'" class="permalink" title="'.get_the_title().'">';
                $html .= '<div class="thumbnail-wrap>';
                    if(has_post_thumbnail()) {
                        $html .= '<img src="'.get_the_post_thumbnail_url().'" class="img-fluid" alt="'.get_the_title().'&nbsp;'.__('Hardware preview', 'textdomain').'">';
                                }
                            $html .= '</div>';
                        $html .= '</a>';
                        $html .= '<a href="'.get_the_permalink().'" class="title-permalink btn btn-outline-primary" title="'.get_the_title().'"><span class="the-title">'.get_the_title().'</span></a>';
                        $html .= '<div class="hardware-metadata">';
                        // Some more metadata can be put here for example
                        $html .= '</div>';
                    $html .= '</div>';
                $html .= '</div>';

                endwhile;
                // Echo it all out
                echo $html;
                wp_reset_postdata();
            ?>
6
  • Thanks thanks man! For your speed, clarity and time. It worked perfect :) – Sema Hernández Feb 15 '20 at 3:04
  • You are welcome :) Glad I could help. – user3135691 Feb 15 '20 at 3:05
  • Is there anyway i can add a pagination? – Sema Hernández Feb 15 '20 at 3:34
  • This answer is completely incorrect. You should not be using custom queries for taxonomy archives. People doing this is the number one mistake people are making when posting about their issues on this site. Something else is wrong. – Jacob Peattie Feb 15 '20 at 4:47
  • Then please enlight us with the answer. Moaning it is wrong is fine, but then not giving a solution is wrong either. – user3135691 Feb 15 '20 at 12:23

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.