Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a number of slugs and items in my custom category:

SLUG - number

video - 5 items

music - 5 items

[no-slug] - 5 items

empty - 0 items

blabla - 0 items

I want to list all non-empty slugs (otpionally with number of items next to them), like:

[ video (5) ] [ music (5) ] [ other (5) ]

(empty and blabla omitted)

Anyone knows how to do that?

All I know is how to get a slug for particular item:

<?php $slug = $terms[0]->slug; ?>

UPDATE

My taxonomy registration:

functions.php

register_taxonomy("op", array("portfolio"), array(
  "hierarchical" => true, 
  "label" => "Categories", 
  "singular_label" => "Category",  
  "rewrite" => true,
));

page-portfolio.php

$taxonomy = "op";
function get_taxonomy_list_html($taxonomy) {
  $term_links = array();
  foreach(get_terms('Category') as $term) {
    if (!empty($term->slug) && $term->count>0) {
      $link = get_term_link($term,$taxonomy);
      $term_links[] = "[ <a href='{$link}'>{$term->name}</a> ({$term->count}) ]";
    }
  }
  return implode(' ',$term_links);
}

Displays nothing :(

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Hi @Wordpressor:

Sounds pretty simple so I'm concerned I misunderstand, but in case I did understand correctly here's a function; just pass the taxonomy to it like this: echo get_taxonomy_list_html('category'):

function get_taxonomy_list_html($taxonomy) {
  $term_links = array();
  foreach(get_terms($taxonomy) as $term) {
    if (!empty($term->slug) && $term->count>0) {
      $link = get_term_link($term,$taxonomy);
      $term_links[] = "[ <a href='{$link}'>{$term->name}</a> ({$term->count}) ]";
    }
  }
  return implode(' ',$term_links);
}
share|improve this answer
    
@Wordpressor: If this is the answer you need, please update your question to use the right terminology: you want to list all terms of your custom taxonomy. –  Jan Fabry Feb 2 '11 at 9:00
    
@Jan Fabry - Good comment. I would have edited his title to be more clear if was certain that's if fact what he wanted. Hopefully he will make the effort to improve his title. –  MikeSchinkel Feb 2 '11 at 9:02
    
@MikeSchinkel, @Jan Fabry , this code doesn't work for me or I'm doing something wrong. I'm a bit confused with slugs and terms, but anyways I'm going to update the first post in a minute so you'll tell me what I do wrong. –  Wordpressor Feb 3 '11 at 18:27
    
@Wordpressor - When you say "the code doesn't work" can you be more specific? That could mean many different things. –  MikeSchinkel Feb 3 '11 at 18:40
    
@Wordpressor - As for "slugs" they are the lowercase version of your term's name. If your taxonomy is 'color' and it's slug is 'colors' and your term's name is "Red" it's slug will be 'red' and your URL for the posts linked to that term would be http://example.com/colors/red/. And hopefully you can tell from context that "term" refers to the thing with which you categorize a post and that has a slug and a name and an associated taxonomy. –  MikeSchinkel Feb 3 '11 at 18:42
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.