【问题标题】:Get Category name from Post ID从帖子 ID 获取类别名称
【发布时间】:2013-06-22 15:13:12
【问题描述】:

是否可以获得给定帖子 ID 的类别的类别名称,以下代码可以获取类别 ID,但是我如何获取名称?

<?php $post_categories = wp_get_post_categories( 4 ); echo $post_categories[0]?>

谢谢!

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    你去get_the_category( $post-&gt;ID ); 将返回你需要遍历数组的帖子的类别数组

    $category_detail=get_the_category('4');//$post->ID
    foreach($category_detail as $cd){
    echo $cd->cat_name;
    }
    

    get_the_category

    【讨论】:

    • 谢谢,但我在哪里定义该代码中的帖子 ID,我想要类别名称。
    • 我不喜欢你必须遍历数组才能获得名称,但它可以工作。
    【解决方案2】:
    echo '<p>'. get_the_category( $id )[0]->name .'</p>';
    

    是您可能正在寻找的。​​p>

    【讨论】:

    • 谢谢!!正是我需要的 - 没有循环 - 更漂亮
    • 谢谢...它返回我们所在的帖子类别名称
    • 真的很好,如果不需要使用循环,例如在单个帖子模板中。不错。
    • 谢谢你。最简单的答案。
    【解决方案3】:

    没有

    <?php get_the_category( $id ) ?>
    

    在循环内就这样做?

    对于外部:

    <?php
    global $post;
    $categories = get_the_category($post->ID);
    var_dump($categories);
    ?>
    

    【讨论】:

    • 这会为我返回一个数组——不是类别的名称
    • 如何将类别数组转换为字符串?
    【解决方案4】:
    function wp_get_post_categories( $post_id = 0, $args = array() )
    {
       $post_id = (int) $post_id;
       $defaults = array('fields' => 'ids');
       $args = wp_parse_args( $args, $defaults );
       $cats = wp_get_object_terms($post_id, 'category', $args);
    
       return $cats;
    }
    

    这里是函数wp_get_post_categories()的第二个参数 您可以传递接收数据的属性。

    $category_detail = get_the_category( '4',array( 'fields' => 'names' ) ); //$post->ID
    foreach( $category_detail as $cd )
    {
       echo $cd->name;
    }
    

    【讨论】:

    • 我更喜欢你的方法(你做的第一个),因为你可以传递任何你想要的分类法。当然,默认为“类别”,但如果您自定义帖子类型和自定义分类法,那么这个 OP 问题的公认答案实际上是不够的。 get_the_category() 将仅搜索分类“类别”。而您发布的方法可以通过分类。打得好,先生。
    【解决方案5】:

    使用get_the_category() 函数。

    $post_categories = wp_get_post_categories( 4 );
    $categories = get_the_category($post_categories[0]);
    var_dump($categories);
    

    【讨论】:

    • @user1937021 你检查输出了吗??
    • 它是一个帖子 ID 吗? $post_categories[0]) ??
    • 嗯 4 是我想要的帖子 id 的类别
    • 这会返回一个数组
    【解决方案6】:
         <?php  
         // in woocommerce.php
         $cat = get_queried_object();
         $cat->term_id;
         $cat->name;
         ?>
    
        <?php
        // get product cat image
            if ( is_product_category() ){
                $cat = get_queried_object();
                $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                $image = wp_get_attachment_url( $thumbnail_id );
                if ( $image ) {
                    echo '<img src="' . $image . '" alt="" />';
                }       
    }
    ?>
    

    【讨论】:

      【解决方案7】:

      您可以通过简单地使用以下方法传递帖子 ID 来单行回显类别名称:

      echo wp_get_post_terms(get_the_ID(), 'category')[0]-&gt;name;

      【讨论】:

        【解决方案8】:

        第一个类别名称(按帖子 ID)

        $first_category = wp_get_post_terms( get_the_ID(), 'category' )[0]->name;
        

        【讨论】:

        • 你为什么使用'product_cat'?
        • 这是类别 slug。如果您的类别 slug 名称是“类别”,那么您可以使用“类别”而不是“产品猫”
        • 另外,woo-commerce 默认为我们提供“product_cat”类别名称
        • 没错,您为什么突然将 WooCommerce 引入其中?海报没有提到 WooCommerce 摊位,这对他们没有帮助,因为他们不想要产品类别
        • 只是一个名字。不管你的类别名称是什么,只要输入你的类别名称,你就会得到结果。我只是回答你的问题,我为什么使用 product_cat 名称
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        • 2011-02-25
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        • 2017-08-02
        • 1970-01-01
        相关资源
        最近更新 更多