我创建了一个名为“map”的自定义post类型,并为其注册了一个新的分类法。现在,我需要通过一个循环,得到“map”的每个术语。到目前为止,我有一个“无效的分类法”,我不明白为什么会这样。除了这个,一切都很好!
如何注册新的分类法(在functions.php中):
function create_map_taxonomy() {
$labels = array(
\'name\' => _x( \'Map_Category\', \'taxonomy general name\', \'textdomain\' ),
\'singular_name\' => _x( \'Map_Category\', \'taxonomy singular name\', \'textdomain\' ),
\'search_items\' => __( \'Search Map_Category\', \'textdomain\' ),
\'popular_items\' => __( \'Popular Map_Category\', \'textdomain\' ),
\'all_items\' => __( \'All Map_Category\', \'textdomain\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Map_Category\', \'textdomain\' ),
\'update_item\' => __( \'Update Map_Category\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Map_Category\', \'textdomain\' ),
\'new_item_name\' => __( \'New Map_Category Name\', \'textdomain\' ),
\'separate_items_with_commas\' => __( \'Separate Map_Category with commas\', \'textdomain\' ),
\'add_or_remove_items\' => __( \'Add or remove Map_Category\', \'textdomain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used Map_Category\', \'textdomain\' ),
\'not_found\' => __( \'No Map_Category found.\', \'textdomain\' ),
\'menu_name\' => __( \'Map_Category\', \'textdomain\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
);
register_taxonomy( \'map_category\', \'map\', $args );
}
add_action( \'init\', \'create_map_taxonomy\', 0 );
这是我在页脚中的代码。php:
<?php
$args = array(
\'post_type\' => \'map\',
);
$map_query = new WP_Query($args);
while ($map_query->have_posts()) : $map_query->the_post();
$img_background = get_field(\'image_background\');
$cat = get_terms(get_the_id(), \'map_category\');
var_dump($cat);
endwhile;
wp_reset_query();
?>
结果总是“分类无效”。但是,当我在管理页面中看到它时,它怎么会是无效的分类法呢?