[wp-hackers] custom taxonomies : how to get the current slug?

Andrew Nacin wp at andrewnacin.com
Wed Jan 19 08:52:13 UTC 2011


On Wed, Jan 19, 2011 at 3:48 AM, Jimmy Roy <jimmy.roy at gmail.com> wrote:

> hi,
> I've a custom post type and a custom taxonomy.
>
> in the sidebar of my site I list the terms, for example:
>
> *Color* (the taxonomy)
>
>   - red (2)
>   - blue (3)
>
> if I click on blue I list all post with associated to the blue term.
>
> in this page (www.example.com/color/blue/, I would like to show the number
> of post (ie 3), I use this piece of code for testing and it works but the
> term name is harcoded:
>
> $taxonomy = "color";
> $term_slug = 'red';
> $term = get_term_by('slug', $term_slug, $taxonomy);
>  echo 'Number of posts: '.$term->count;
>
> so my question is how to get the current slug?
>
> (and sorry for my bad english!)
>
> --
> Jimmy | www.jimmyroy.fr


A few ways -- I would use this:

$term = $wp_query->get_queried_object();

Then $term is a term object with ->slug, ->name, ->count, etc.

--
Nacin


More information about the wp-hackers mailing list