在帖子列表中通过税务查询获取分类名称



我有ajax类别过滤,我用它来获取分类id并按分类列出帖子。现在我想在列表视图中显示当前的分类名称。

我试图在<div class="list">的上面添加<h4>get_cat_name((int)$category)</h4>,但没有显示任何内容。帖子将被正确列出,这取决于我在前端单击的分类法。如何在帖子列表下方打印类别名称?

function ajax_filtering() {
$category = esc_html($_POST['category']);
$args = array(
'post_type' => 'post',
'orderby' => 'name',
'order' => 'ASC'
);
if ( isset( $category ) ) :

$args['tax_query'] = 
array( 
array( 
'taxonomy' => esc_html($_POST['taxonomy']),
'field' => 'id', 
'terms' => array((int)$category) 
) ); 
endif;

$the_query = new WP_Query( $args );
?>
<div class="list">
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post(); 

?>
<div class="post">
<a href="<?php the_permalink(); ?>" class="post-link">
<?php the_post_thumbnail('post-thumb'); ?>
<h4 class="post-name"><?php the_title(); ?></h4>
</a>
</div>  

您可以使用get_term_by()。将以下代码添加到<div class="list">的上面

$category = get_term_by('id', $category, $_POST['taxonomy']);
echo '<h4>'.$category->name.'</h4>';

有用链接

  • get_term_by((

最新更新