我想在RSS提要中显示分类法。我可以根据显示自定义字段Add Custom Fields to Custom Post Type RSS (感谢那篇文章的所有贡献者),但我无法显示包含分类法值的字段。以下是我使用的代码:
add_action(\'rss2_item\', \'yoursite_rss2_item\');
function yoursite_rss2_item() {
if (get_post_type()==\'listings\') {
$fields = array(
\'listing_category\',
\'listing_type\',
\'listing_bedrooms\',
\'listing_city\',
);
$post_id = get_the_ID();
foreach($fields as $field)
if ($value = get_post_meta($post_id,$field,true))
echo "<{$field}>{$value}</{$field}>\\n";
}
}
以下是未显示的两个分类字段:
“listing\\u类别”-“待售”、“租赁”或两者兼有(多个值)“listing\\u type”-1个包含“Condo”、“House”、“Land”或“Building”的值非常感谢您的帮助。
最合适的回答,由SO网友:danielck 整理而成
get_post_meta
仅适用于自定义字段。如果listing_category
和listing_type
实际上是分类法,您需要使用get_the_terms
相反生成的代码如下所示:
add_action(\'rss2_item\', \'yoursite_rss2_item\');
function yoursite_rss2_item() {
if (get_post_type()==\'listings\') {
$fields = array(
\'listing_bedrooms\',
\'listing_city\',
);
$post_id = get_the_ID();
foreach($fields as $field) {
if ($value = get_post_meta($post_id,$field,true)) {
echo "<{$field}>{$value}</{$field}>\\n";
}
}
$taxonomies = array(
\'listing_category\',
\'listing_type\'
);
// Loop through taxonomies
foreach($taxonomies as $taxonomy) {
$terms = get_the_terms($post_id,$taxonomy);
if (is_array($terms)) {
// Loop through terms
foreach ($terms as $term) {
echo "<{$taxonomy}>{$term->name}</{$taxonomy}>\\n";
}
}
}
}
}
有关详细信息,请参阅文档
get_the_terms
此处:
http://codex.wordpress.org/Function_Reference/get_the_terms