Get costom post type taxonomies

returning array of taxonomies about a custom post type like

array(
[0] =>taxonomy_name1,
[1] =>taxonomy_name2
)

Code

function _get_post_taxonomies($post_type) {
    // Passing an object
    // Why another var?? $output = 'objects'; // name / objects
    $taxonomies = get_object_taxonomies($post_type);
    /*// Passing a string using get_post_type: return (string) post, page, custom...
    $post_type  = get_post_type($post);
    $taxonomies = get_object_taxonomies($post_type, 'objects');*/
    return (array) $taxonomies; // returning array of taxonomies
}

and  them

$taxonomies = _get_post_taxonomies(get_post_type()); //
$term_list = wp_get_post_terms($post->ID, $taxonomies[0]); //get term list by first taxonomy

will get a array like:

Array
(
    [0] => stdClass Object
        (
            [term_id] => 9
            [name] => Special Events
            [slug] => special-events
            [term_group] => 0
            [term_taxonomy_id] => 9
            [taxonomy] => portfolio_cat
            [description] => 
            [parent] => 0
            [count] => 1
        )

)

and them, you can get these terms link like this:

echo '<a href="' . get_term_link($term_list[0]->slug, $taxonomies[0]) . '">' . $term_list[0]->name . '</a>';

actrully, you can use it here:

Comments are closed.