【问题标题】:WordPress Plugin "Search and Filter" - display Custom Post Type Categories in results.php fileWordPress 插件“搜索和过滤” - 在 results.php 文件中显示自定义帖子类型类别
【发布时间】:2021-01-06 06:51:34
【问题描述】:

我正在从 Search and Filter Pro 插件 (WordPress) 操作 results.php 文件。我还尝试显示每种自定义帖子类型的类别。但我在前端看到的只是“数组”而不是类别名称。这是我当前的代码:

if ( $query->have_posts() )
{
    ?>

<div class="pagination">
    
    <div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    <div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php
        /* example code for using the wp_pagenavi plugin */
        if (function_exists('wp_pagenavi'))
        {
            echo "<br />";
            wp_pagenavi( array( 'query' => $query ) );
        }
    ?>
</div>

<?php

while ($query->have_posts())
{
    $query->the_post();
    
    $post_id = get_the_ID();
    $post_category = get_the_category();
    $description = get_field('product_description', $post_id);
    
    ?>

    <div class="product-div">
        
        <?php 
            if ( has_post_thumbnail() ) {
                echo '<p>';
        ?>
        <?php the_post_thumbnail("small");?>
        <?php
                echo '</p>';
            }
        ?>
        <h3><?php echo $post_category; ?></h3>
        <h2><?php the_title(); ?></h2>
        <?php echo $description;?>

        
    </div>
    
    <?php
    }
    ?>

   <?php
   }
   else
  {
   echo "No Results Found";
   }
  ?>

我做错了什么?

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    查看函数get_the_category 根据reference page 返回一个WP_Term 对象数组。试试&lt;?php echo $post_category[0]-&gt;name; ?&gt;,或者如果帖子分配了多个类别,您可以循环访问:

    foreach($post_category as $category){
        echo $category->name;
    }
    

    【讨论】:

    • 这正是我所需要的。像魅力一样工作。非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2020-07-21
    • 2017-02-11
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多