WordPress Category Exists?

Already tried in WordPress to check if a category exists? There is no Template Tag so you need to create your own solution.
I have 3 different solutions prepared, although one solution only works within the framework of the admin area.

The individual solutions are not explained in depth but surely understandable if you read the code. Alternatively, you can also create a custom function and then use it, like a separate Template Tag for the theme. Therefore put this function in the functions.php of the theme.

In order to understand the code, I have as an example either the category called „Uncategorized“ , or the ID 1. These values must of course be adjusted to your theme.



Posted

in

by

Comments

6 responses to “WordPress Category Exists?”

  1. Peter Avatar

    I read this yesterday, figured I’d never use it, and then later that night realized that I needed exactly this functionality.

    I was putting together a custom feature for a client to automatically post some stuff, and I needed it to create categories if they didnt exist – I figured I’d just loop through the desired categories and check if each of them exists (with category_exists), then create them if necessary – but then I found another function to do it for me –
    function wp_create_categories($categories, $post_id = ”) – it loops through the $categories array and creates the categories if they dont exist, and then applies them to post $post_id (if supplied).

    Anyway, I never would have found it if it werent for this post – Thanks!

  2. Alex Avatar

    Hey Peter, your welcome. Thanks for letting us know for what practical purpose you also could need it.

  3. jez Avatar

    did they take get_categories out with the latest release of wordpress 2.8 (beta) ?

    I used to have

    $index_cats = ‘16,7,5,10’; // What categories to show on Index page
    $args = array(
    ‘orderby’ => ‘RAND’, //ID
    ‘order’ => ‘DESC’, // change cat ids so to get right order
    ‘hierarchical’ => 0,
    ‘include’ => $index_cats ); //Which categories to include, set on the theme options page

    $cats = get_categories($args); //sets up everything to be looped through

    foreach ($cats as $key => $category) { // loops through categories in array and creates a section for each

    $catID = $category->cat_ID; //sets catID to category ID number
    $catName = $category->cat_name; // sets catName to category Name
    $cat = $cats[$key]; // Sets category

    and then echo it later-on in my code,
    and with wp 2.7.4 it worked, with wp 2.8 it just returns “blank”.

    any clues much appreciated,
    gerne auch in deutsch.

  4. Michael Avatar

    Hm, i see only a problem with orderby => RAND. Try orderby => ID or name. See http://codex.wordpress.org/Function_Reference/get_categories

  5. Simon Avatar

    Hi Michael,

    I’m having a similar problem with my themes. You can see the problem in WordPress 2.8 if you try (in the functions.php or a theme file)

    print_r(get_categories('hide_empty=0'));

    I use an array of categories for a select input field in my theme options page. Since WordPress 2.8 it does not work any more.

    The functions returns:

    Array
    (
        [errors] => Array
            (
                [invalid_taxonomy] => Array
                    (
                        [0] => Invalid Taxonomy
                    )
            )
        [error_data] => Array
            (
            )
    )

    Another discussion on this:
    http://themeforest.net/forums/thread/wp-28-compatible/13432

    Anybody has an idea how to solve this issue? Thanks in advance!

    Simon

  6. […] Pour information, consultez la page sur ce sujet sur wpengineer.com. […]