【问题标题】:Get taxonomy name by tax-query in posts list通过帖子列表中的税收查询获取分类名称
【发布时间】: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')

标签: php ajax wordpress


【解决方案1】:

您可以使用get_term_by()。在&lt;div class="list"&gt;上方添加以下代码

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

有用的链接

【讨论】:

  • 欢迎...很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2011-02-25
  • 2013-02-05
相关资源
最近更新 更多