【问题标题】:Is there a way to query all custom post type tags and return the slug of each? [closed]有没有办法查询所有自定义帖子类型标签并返回每个标签? [关闭]
【发布时间】:2021-09-17 08:37:11
【问题描述】:

有没有办法查询所有自定义帖子类型标签并返回每个标签的 slug?

这是我当前返回标签名称的代码:

$current_article_tags = wp_get_object_terms(get_the_ID(), 'knowledge_hub_tag', array('fields' => 'names'));

我尝试将名称更改为 slug,但没有成功:

$current_article_tags = wp_get_object_terms(get_the_ID(), 'knowledge_hub_tag', array('fields' => 'slug'));

【问题讨论】:

标签: php arrays wordpress slug taxonomy-terms


【解决方案1】:

你的方法就差不多了。为了得到只是蛞蝓我建议你包装wp_get_object_terms函数如下。

function get_object_term_slugs($object_id, $taxonomy){
    $terms = wp_get_object_terms($object_id, $taxonomy);
    return array_map(function($term){
        return $term->slug;
    }, $terms);
}

$current_article_slugs = get_object_term_slugs(get_the_ID(), 'knowledge_hub_tag');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多