【发布时间】:2021-04-27 17:28:48
【问题描述】:
我有 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>
【问题讨论】:
-
echo$category的值,看看它打印了什么。 -
试试这个:get_term_by('id', $category, 'category')