Display all taxonomy based on post type

2019-06-24 03:17发布

I just want to display a taxonomy term based on post type so that I can display and filter the items based on their taxonomy.

My code below displays EVERY taxonomy. I want it to be only a specific post type Publication

<?php
     $taxonomy = 'articletype';

     $tax_terms = get_terms( $taxonomy, array(
        'post_type' => 'publication'
     ) );

 ?>
 <?php
     foreach ($tax_terms as $tax_term) {
         ?>
     <button class="button" data-filter=".<?php echo $tax_term->slug;?>" >
         <?php echo $tax_term->name;?>
     </button>
 <?php } ?>

标签: php wordpress
2条回答
叛逆
2楼-- · 2019-06-24 04:05

I'm not sure if it's still doable in your case (depends on if your site is still on dev or has too much content already), but I think it would be better to create custom taxonomy for your custom post type, it would make it easier for you to orgaanize your content.

I tried to look for a function in the wp codex that sorts taxonomy by post type but I couldn't find it.

查看更多
男人必须洒脱
3楼-- · 2019-06-24 04:06

Try using Codex Function Reference/get object taxonomies.

Usage:

<?php get_object_taxonomies( $object, $output ); ?>

Parameters:

$object: (array|string|object) (required): Name of the post type, or a post object (row from posts).

$output: (string) (optional): The type of output to return, either taxonomy 'names' or 'objects'.

Return Values:

(array): All taxonomy names or objects for the given post type/post object.

also you can visit the original codex page to get examples and more info.

查看更多
登录 后发表回答