Iladarsda
Iladarsda

Reputation: 10696

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>';
}

Upvotes: 2

Views: 18089

Answers (3)

Peter Hadorn
Peter Hadorn

Reputation: 21

Change

'type' => 'company', // your custom post type

to

'taxonomy' => 'yourtaxonomyname', // your custom taxonomy

see also http://codex.wordpress.org/Function_Reference/get_categories

Upvotes: 2

Iladarsda
Iladarsda

Reputation: 10696

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;​

Upvotes: -2

Xhynk
Xhynk

Reputation: 13840

try changing

'type' => 'company', //your custom post type

to

'post_type' => 'company', //your custom post type

Upvotes: 6

Related Questions