0

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 :(

1 Answer 1

2

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);
}
12
  • @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
    Commented Feb 2, 2011 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. Commented Feb 2, 2011 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. Commented Feb 3, 2011 at 18:27
  • @Wordpressor - When you say "the code doesn't work" can you be more specific? That could mean many different things. Commented Feb 3, 2011 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. Commented Feb 3, 2011 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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