下文給各位整理一篇關于wordpress獲取自定義post_type的分類例子,希望這個例子可以幫助到各位,雖然簡單但總會有一些作用的.
在自己建立一種post-type的文件類型后,然后分類也是自己用register_taxonomy來自定義的.
這個時候我在用如下代碼:
- <?php the_category(', '); ?>
來獲取當前文章的分類,取到的數據是空,百度后用 query_posts 指定post-type.獲取還是不ok,最后只能通過添加個function來獲取,代碼如下:
- /**
- * 獲取當前自定義類型的,分類名稱!
- * @return string
- */
- function custom_taxonomies_terms_links(){
- //根據當前文章ID獲取文章信息
- $post = get_post( $post->ID );
- //獲取當前文章的文章類型
- $post_type = $post->post_type;
- //獲取文章所在的自定義分類法
- $taxonomies = get_object_taxonomies( $post_type, 'objects' );
- $out = array();//開源軟件:Vevb.com
- foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
- $term_list = wp_get_post_terms($post->ID, $taxonomy_slug, array("fields" => "all"));
- echo $term_list[0]->name; //顯示文章所處的分類中的第一個
- }
- return implode('', $out );
- }
新聞熱點
疑難解答
圖片精選