您的当前位置:首页 > 知识博客 > wordpress建站

WordPress获取文章所属分类法函数:wp_get_post_terms

时间:2024-03-11

WordPress函数wp_get_post_terms可用于获取指定文章所属的分类法。

wp_get_post_terms( int $post_id, string|string[] $taxonomy = 'post_tag', array $args = array() )

函数参数

$post_id

整数

文章的ID

$taxonomy

字符串,默认值:post_tag

分类法名称,默认为标签。可选值:category

$args

数组,参数可用值参考get_categories()函数的$args参数说明

函数返回值

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 10
            [name] => WP函数
            [slug] => wordpress-functions
            [term_group] => 0
            [term_taxonomy_id] => 10
            [taxonomy] => post_tag
            [description] => 
            [parent] => 0
            [count] => 101
            [filter] => raw
        )

)

函数使用示例

function post_taxonomy_slug_array($tax_name = 'category') {
    $terms = wp_get_post_terms(get_the_ID(), $tax_name, array('fields' => 'slugs'));
    if(!empty($terms) && !is_wp_error($terms)) {
        $term_array = array();
        foreach($terms as $term) {
            $term_array[] = $term;
        }
        return $term_array;
    }
    return '';
}

扩展阅读

wp_get_post_terms()函数位于:wp-includes/post.php

相关函数:

  • wp_get_post_tags()

  • wp_get_post_categories()

  • wp_get_object_terms()


欢迎咨询/Welcome to inquire
tel/vx:18842938855
qq:1685522781
email:1685522781@qq.com

Copyright © 2019-2024 java.fit


Whatsapp
Powered by RRZCMS