the_category 功能无法正常运行;类别没有显示它们应该在的位置

the_category function is not working properly; the categories are not showing where they are supposed to be

我的投资组合页面中有这个相关帖子部分,我正在尝试使用“the_category”函数为每个 post 生成类别。在这种情况下,左边的 post 有类别“照片、测试”,其余的有类别“博客”。

该功能似乎运行正常,但正如您在附图中看到的那样,类别显示在标题上方。

我希望在“listing_meta”块中生成类别。 (在评论旁边)

    <div class="blog_content">          
        <div class="listing_meta">
           <span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>     
           <span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
           <span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
           <span>'. the_category(', ') .'</span>
        </div>
        <div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
    </div>

有解决办法吗?

谢谢。

------------------------已编辑-------------------- ------------

我尝试了“get_the_category”函数,但现在 returns 所有类别都作为 ARRAY,如图所示。


    <div class="blog_content">          
        <div class="listing_meta">
           <span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>     
           <span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
           <span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
           <span>'. get_the_category() .'</span>
        </div>
        <div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
    </div>

get_the_category()

https://developer.wordpress.org/reference/functions/get_the_category/

这就是你想要的功能。如果你使用

the_category()

它会立即回显,然后在您的其余内容之前回显。

----------------编辑------------ 如果返回多个类别,您可以使用 foreach:

将数组转换为以逗号分隔的类别列表
//get all the categories
$categories = get_the_category();

//begin to assemble our string/html we want return/output
$returnedString = '<div class="blog_content">          
    <div class="listing_meta">
    <span>'. esc_html(get_the_time(get_option('date_format'))) .'</span>     
    <span class="blog_post_author">'. __('by', 'gt3_builder') .' <a href="'.get_author_posts_url( get_the_author_meta('ID')).'" class="text-capitalize">'.get_the_author_meta('display_name').'</a></span>
    <span class="comments"><a href="' . get_comments_link() . '">'. get_comments_number(get_the_ID()) .'</a></span>
    <span>';
//loop over the array of category objects
foreach($categories as $category){
    //append each category name to the returned string
    $returnedString .= $category->name . ',';
}
//add the remaining HTML
$returnedString .= '</span>
    </div>
    <div class="blog_post_title"><h2><a href="'. get_permalink() .'">' . get_the_title() . '</a></h2><div class="blog_post_format_label"></div></div>
    </div>';