[wp-hackers] if category exists

Glenn Ansley glenn at glennansley.com
Fri Mar 20 12:54:37 GMT 2009


If you're in the admin area you can use the following conditional:
if ( category_exists('category_name') ) { ... }

If you're not in the admin, you just have to reference is_term:
if ( is_term( 'category_name' , 'category' ) ) { ... }

In the second example, "category" needs to remain category, but
category_term is the title of the category your checking for.

Glenn Ansley
Custom WordPress Plugins
http://fullthrottledevelopment.com

On Thu, Mar 19, 2009 at 3:37 PM, Austin Matzko <if.website at gmail.com> wrote:
> 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;
> }
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list