wordpress 如何获取自定义帖子类型的分类值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22564382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:30:40  来源:igfitidea点击:

How to get the taxonomy values of a custom post type

wordpresscustom-post-typecustom-taxonomy

提问by user2091936

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

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

So far I got the following:

到目前为止,我得到了以下信息:

<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.

我如何获得它的分类法?我尝试了多种方法,但似乎都失败了,而且越来越困惑。

采纳答案by MikO

Check this function: wp_get_post_terms()

检查这个函数:wp_get_post_terms()

Assuming your custom post type Case Studysupports two taxonomies called countryand subject, you can try something like this:

假设您的自定义帖子类型Case Study支持两个名为countrysubject 的分类法,您可以尝试以下操作:

<?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; ?>

Your output would be something like:

您的输出将类似于:

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

回答by Nurealam Sabbir

assume: I register a taxonomy with custom post type name publication_category.

假设:我使用自定义帖子类型名称Publication_category注册了一个分类法。

On your custom post type template write :

在您的自定义帖子类型模板上写:

$terms = get_the_terms( $post->ID, 'publication_category' );
if ($terms) {
    foreach($terms as $term) {
      echo $term->name;
    } 
}

回答by Ollo

Have you tried using <?php get_taxonomies() ?>?

你试过使用<?php get_taxonomies() ?>吗?

If your looking for specific taxonomies that function has optional arguments you can pass in to control the output. See documentation here : http://codex.wordpress.org/Function_Reference/get_taxonomies

如果您正在寻找该函数具有可选参数的特定分类法,您可以传入以控制输出。请参阅此处的文档:http: //codex.wordpress.org/Function_Reference/get_taxonomies

回答by Ed Vieira

Just in case if it could help someone, I used "the_taxonomies()" function inside a loop of a custom post type.

以防万一它可以帮助某人,我在自定义帖子类型的循环中使用了“the_taxonomies()”函数。

        <?php

        while ( have_posts() ) : the_post();    
          $custom_post = get_post_meta( get_the_ID() );       
          //
        ?>

        //html
        //and stuff

        <?php the_taxonomies(); ?>

        <?php
          endwhile;
        ?>


 the result was:

   Taxonomy-name: {Taxonomy-term}. <-- as a link