Search code examples
wordpresstaxonomy

Wordpress, list all taxonomies for custom post type with permalink


Is there any easy way to list all taxonomies for custom post type with permalinks?

taxonomy=title&post_type=company

The following does not seams to work, it is listing the categories for posts only:

$args = array (
    'type' => 'company', //your custom post type
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => 0 //shows empty categories
);
$categories = get_categories( $args );
foreach ($categories as $category) {    
    echo $category->name;
    $post_by_cat = get_posts(array('cat' => $category->term_id));

    echo '<ul>';
    foreach( $post_by_cat as $post ) {
        setup_postdata($post);
        echo '<li><a href="'.the_permalink().'">'.the_title().'</a></li>';
    }
    echo '</ul>';
}

Solution

  • This should work:

    $index_query = new WP_Query(array('post_type' => 'company', 'posts_per_page' => '-1', 'order' => 'DESC'));
    
    while ($index_query->have_posts()) : $index_query->the_post();
    
        $taxonomy_ar = get_the_terms($post->ID, 'tax-name');
    
        $output = '<span class="btn">';
        foreach($taxonomy_ar as $taxonomy_term) {
            $output .= '<a href="'.get_term_link($taxonomy_term->slug, 'title').'">'.$taxonomy_term->name.' <span class="label">'.$taxonomy_term->count.'</span></a> ';
        }
        $output .= '</span>';
    
        echo $output;
    
    endwhile;​