Wordpress获取自定义分类的术语

Wordpress获取自定义分类的术语,wordpress,taxonomy,Wordpress,Taxonomy,在归档页面中,我试图显示自定义分类法(称为位置)以及每个文章标题、类别和标记。我无法让“让条款”生效 文章的标题、类别和标签工作得很好。分类法不起作用 <div class="post-listing"> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <p><?php

在归档页面中,我试图显示自定义分类法(称为位置)以及每个文章标题、类别和标记。我无法让“让条款”生效

文章的标题、类别和标签工作得很好。分类法不起作用

<div class="post-listing">
        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <p><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
        <p><?php $tags = get_the_tags(); foreach($tags as $tag) { echo "$tag->name"; } ?></p>
        <p><?php $terms = get_the_terms( 'locations' ); foreach($terms as $term) { echo "$term->name"; } ?></p>
</div>

有什么想法吗?它快把我逼疯了

get_函数有两个参数。它们是$post和$taxonomy。与其他可以跳过循环内$post(post对象或post ID)参数的“get_the_xxxx”函数不同,您必须将post对象或post ID添加到第一个参数中

我认为你应该写信

$terms = get_the_terms( $post, 'locations' );
而不是

$terms = get_the_terms( 'locations' );
文档:

需要一个post ID作为第一个参数。我想你是想用它来代替
$terms = get_the_terms( 'locations' );