首先,您没有对$term变量做任何操作。您正在查询它,但它就在那里,等待被提取。
在分类法模板中尝试以下代码,名为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();
?>