如何获取自定义帖子类型的分类法值 [英] How to get the taxonomy values of a custom post type

查看:71
本文介绍了如何获取自定义帖子类型的分类法值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新模板,该模板将获取所有自定义帖子类型(案例研究)内容,包括与之关联的分类标准值。

I am creating a new template that will get all the custom post type (Case Studies) content, including the taxonomies values associated with it.

到目前为止,我知道了

<section>
<h1><?php _e( 'posts', 'casestudies' ); ?></h1>
<?php get_template_part('loop'); ?>
<?php
$args = array('post_type' => 'casestudies', 'posts_per_page' => 3);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h2><?php the_title(); ?></h2>
<p>Meta: <?php the_meta(); ?></p>
<p>Excerpt: <?php the_excerpt(); ?></p>
<p>what_to_put_here_to_get_taxonomies_values????</p>
<?php endwhile; ?>

<?php get_template_part('pagination'); ?>
</section>

如何获得分类法?我已经尝试了多种方法,但似乎都失败了,而且变得更加困惑。

How do I get the taxonomy of it? I have tried multiple things but all seemed to fail and just getting more confused.

推荐答案

检查此功能: wp_get_post_terms()

Check this function: wp_get_post_terms()

假设您的自定义帖子类型案例研究支持两种分类法,分别称为 country subject ,您可以尝试如下操作:

Assuming your custom post type Case Study supports two taxonomies called country and subject, you can try something like this:

<?php $terms = wp_get_post_terms( $query->post->ID, array( 'country', 'subject' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>

您的输出将类似于:

Country: United Kingdom
Subject: Biology
Subject: Chemistry
Subject: Neurology

这篇关于如何获取自定义帖子类型的分类法值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆