如何将自定义 post 分类法名称仅放入数组?

How to put custom post taxanomies name only to array?

我在 wordpress 中有自定义 post 类型。自定义 post 有分类法。

<?php
    $customQuery = new WP_Query([
        'post_type' => 'custom'
    ]);
    while ($customQuery->have_posts()) {
        $customQuery->the_post();
        print_r(the_taxonomies());
    }
?>

结果:

Array
(
    [portfolio_tag] => Taxonomies: <a href="http://localhost/wordpress/custom_tag/app/">App</a> and <a href="http://localhost/wordpress/custom_tag/developer/">Developer</a>.
)

如何在 wordpress 中获取所有分类名称到数组?

Array
(
   'App',
   'Developer'
)

您可以使用 wp_get_object_termswp_get_post_terms 如果它不起作用,则将函数 wp_get_object_terms 替换为 wp_get_post_terms

wp_get_object_termswp_get_post_terms函数中有2个参数,第一个是post id,第二个是custom-taxonomy-name,所以你可以在这里传递第二个参数

知识

https://codex.wordpress.org/Function_Reference/wp_get_post_terms

https://codex.wordpress.org/Function_Reference/wp_get_object_terms

<?php
    $customQuery = new WP_Query([
        'post_type' => 'custom'
    ]);
    while ($customQuery->have_posts()) {
        $customQuery->the_post();
        $arr_get_terms = wp_get_object_terms($customQuery->ID, 'custom-taxonomy-name-here');
        print_r($arr_get_terms);
    }
?>