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(); ?>
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(); ?>
Share
Improve this question
edited Feb 15, 2020 at 2:48
Sema Hernández
asked Feb 15, 2020 at 1:34
Sema HernándezSema Hernández
1611 gold badge3 silver badges11 bronze badges
2
- Are you trying to display a list of posts in each term, or a list of terms themselves? – Jacob Peattie Commented Feb 15, 2020 at 2:00
- Trying to display a list of posts in each term, thanks for answer. – Sema Hernández Commented Feb 15, 2020 at 2:23
1 Answer
Reset to default 0Well, 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().' '.__('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();
?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744746624a4591338.html
评论列表(0条)