我有一个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非常陌生,不知道如何将这些数据检索到我的前端。我在这里做错什么了?
发布于 2020-04-24 04:47:34
如果要显示唯一的类别或分类名称而不是get_posts,则必须使用get_terms。
检查这段代码。
// 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
}
}https://stackoverflow.com/questions/61384938
复制相似问题