我在我的WordPress網站中創建了自定義帖子類型。並且我爲該自定義帖子類型添加了許多帖子。使用帖子id wordpress顯示分配給自定義帖子類型的所有類別
我想要顯示分配給該自定義帖子類型中每個帖子的所有類別。請幫幫我。 thankz ...
我在我的WordPress網站中創建了自定義帖子類型。並且我爲該自定義帖子類型添加了許多帖子。使用帖子id wordpress顯示分配給自定義帖子類型的所有類別
我想要顯示分配給該自定義帖子類型中每個帖子的所有類別。請幫幫我。 thankz ...
你可以試試:
$terms = get_the_terms($post->ID, 'your_taxonomy_name');
http://codex.wordpress.org/Function_Reference/get_the_terms
,但你必須設置一個分類爲您CPT第一:
http://codex.wordpress.org/Function_Reference/register_taxonomy
你可以試試下面的代碼
foreach (get_posts('numberposts=-1') as $post) {
wp_set_object_terms($post_id, 'category_name', 'category');
}
使用WordPress的功能:
has_term:http://codex.wordpress.org/Function_Reference/has_term
和get_the_terms:http://codex.wordpress.org/Function_Reference/get_the_terms
它使用has_term(),這樣你就不會在不使用自定義標籤的帖子得到的錯誤是非常重要的。
get_the_terms()獲取當前帖子ID的所有術語,在循環中使用它。
例如:
<ul class="post-entry-meta">
<?php if(has_term('','your taxonomy'))
{
$terms = get_the_terms($post->ID , 'your taxonomy');
foreach ($terms as $term)
{echo '<li class="taxonomy-list">', $term->name , '</li>' ;}
}?>
</ul>
<?php
$custom_post_taxonomies = get_object_taxonomies('your_taxonomy_name');
if(count($custom_post_taxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,
'taxonomy' => $tax,
'title_li' => ''
);
wp_list_categories($args);
}
}
?>
請您分類名稱替換 「your_taxonomy_name」。它工作得很好。
這將列出所有類別。而不是分配給特定職位的人員。 – honk31
我爲初學者創建了一個教程網站http://www.e2soft.com/blog&www.e2soft.com/forum – csehasib