Javascript 在wordpress中使用分类法获取帖子

Javascript 在wordpress中使用分类法获取帖子,javascript,php,wordpress,wordpress-rest-api,Javascript,Php,Wordpress,Wordpress Rest Api,我的电脑中本地安装了wordpress应用程序。在wordpress管理员中,我在帖子下面有一个国家标签。为了更好地理解,我将附上一张图片 我想写一个函数来获取我前端的国家值。为此,我编写了这样一个函数 public function get_destinations() { $bookings = get_posts( array( 'taxonomy'=> 'countr

我的电脑中本地安装了wordpress应用程序。在wordpress管理员中,我在帖子下面有一个国家标签。为了更好地理解,我将附上一张图片

我想写一个函数来获取我前端的国家值。为此,我编写了这样一个函数

    public function get_destinations()
    {

            $bookings = get_posts(
                array(
                    'taxonomy'=> 'country',
                    'numberposts' => -1,
                )
            );
            return $bookings;
    }
但由于某些原因,此函数返回数据库中的所有帖子。我只想知道国家的名字

我从我的本地url中找到了分类法,它是

http://localhost/my_project/wp-admin/edit-tags.php?taxonomy=country

我是wordpress的新手,不知道如何将这些数据检索到我的前端。我在这里做错了什么?

如果您想显示唯一的类别或分类名称,而不是get\u posts您必须使用get\u术语

检查此代码

// Get the taxonomy's terms
    $terms = get_terms(
        array(
            'taxonomy'   => 'country',
            'hide_empty' => false, // show category even if dont have any post.
        )
    );

    // Check if any term exists
    if ( ! empty( $terms ) && is_array( $terms ) ) {
        // Run a loop and print them all
        foreach ( $terms as $term ) { ?>
            <a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
                <?php echo $term->name; ?>
            </a><?php
        }
    }
//获取分类法的术语
$terms=获取条款(
排列(
“分类法”=>“国家”,
'hide_empty'=>false,//即使没有任何帖子也显示类别。
)
);
//检查是否存在任何术语
if(!empty($terms)&&is_数组($terms)){
//运行一个循环并将它们全部打印出来
foreach($terms作为$term){?>