简体   繁体   中英

Get the terms of a custom taxonomy of custom post type in an array

I have a custom post type 'events' and a taxonomy-'Event types'. How can i get all the terms that belong to custom taxonomy 'Event types' , in an array . I use wp_list_categories($args) but it gives the output with each type in ali tags.

Please help me out

code for registering taxonomy

<?php
function event_init() {
    // create a new taxonomy
    register_taxonomy(
        'Event types',
        'events',       
        array(
            'labels' => array(
                            'name'=>'Event types',
                            'add_new_item'=>'Add New Event types ',
                            'new_item_name'=>"New Event types"              
                            ),

            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'rewrite' => array( 'slug' => 'event-type' ),



             )
    );
}
add_action( 'init', 'event_init' );

?>
$terms = get_terms( 'your_taxonomy_name' );

This will get you an array of term objects. Note your taxonomy name must be lowercase letters and underscores only.

Ref: http://codex.wordpress.org/Function_Reference/get_terms

$terms = get_terms( 'Event types' );

This will work for you. For better experience don't use capital letters and spaces in names of the taxonomy or any other variables names.

Ref: https://developer.wordpress.org/reference/functions/get_terms/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM