[wp-hackers] if category exists

Austin Matzko if.website at gmail.com
Thu Mar 19 19:37:55 GMT 2009


On Thu, Mar 19, 2009 at 2:09 PM, Daniel Cameron <dan at sproutventure.com> wrote:
> Basically I have category links hardcoded into my theme template like so: <li
> id="nav-linkt" <?php if (is_category('cat')) { echo "class=\"active_link\""
> ; } ?>>
> <a href="<?php echo
> get_category_link(get_category_by_slug('cat'));?>">Cat</a>
> </li>
>
> which is fine but if the category doesn't exist (in the example above 'cat')
> then the theme bombs.
>
> Is there a way to wrap this list item with something like <?php if
> category_exists { ?
>
> I know that function doesn't exist but anything similar without having to
> hard code a query?

To see if a category with ID 123 exists, you could do:

$cat = get_category(123);
if ( ! empty( $cat ) ) {
// do something;
}


More information about the wp-hackers mailing list