【问题标题】:the_category function is not working properly; the categories are not showing where they are supposed to bethe_category 功能无法正常工作;类别没有显示它们应该在哪里
【发布时间】:2023-03-23 05:22:01
【问题描述】:

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

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

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

    <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”,但现在它以 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>

【问题讨论】:

    标签: php wordpress function categories


    【解决方案1】:
    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>';
    

    【讨论】:

    • 谢谢。我试过了,它们完全在街区内。但是,我还有另一个问题。现在,它以 ARRAY 的形式返回所有类别,如第二张图片所示。(我已经更新了帖子)我对 PHP 不太了解,非常感谢您的帮助。
    • 您没有包括围绕 HTML 发生的所有代码,因为看起来 HTML 是从函数返回或设置到变量中的,所以我会更新我的答案,但是你得稍微适应一下。
    猜你喜欢
    • 2013-10-29
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2015-07-05
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多