Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this template I purchased and I'm trying to customize.

I added a new taxonomy category called location and I'm trying to replace the default categories being loaded in the theme with my new taxonomy categories.

here's the current code and where it loads the categories:

 $cats_array = get_categories('hide_empty=0');
 $pages_array = get_pages('hide_empty=0');
 $site_pages = array();
 $site_cats = array();

 foreach ($pages_array as $pagg) {
$site_pages[$pagg->ID] = htmlspecialchars($pagg->post_title);
$pages_ids[] = $pagg->ID;
 }

 foreach ($cats_array as $categs) {
$site_cats[$categs->cat_ID] = $categs->cat_name;
$cats_ids[] = $categs->cat_ID;
 }


    array( "name" => "Featured Category",
           "id" => $shortname."_feat_cat",
           "type" => "select",
           "options" => $site_cats,
           "desc" => "description"),

Ideally, this change should work:

$cats_array = get_categories('hide_empty=0&taxonomy=location');

but it doesn't! I'm obviously missing something and don't know how to fix this. Any help is appreciated.

Here's the register taxonomy part:

    // Register custom taxonomy
    register_taxonomy(  "location", 
                        array(  "woo_estate"    ), 
                        array ( "hierarchical"      => true, 
                                "label"             => "Locations", 
                                'labels'            => array(   'name'              => __('Locations'),
                                                                'singular_name'     => __('Location'),
                                                                'search_items'      => __('Search Real Estate'),
                                                                'popular_items'     => __('Popular Locations'),
                                                                'all_items'         => __('All Locations'),
                                                                'parent_item'       => __('Parent Location'),
                                                                'parent_item_colon' => __('Parent Location:'),
                                                                'edit_item'         => __('Edit Location'),
                                                                'update_item'       => __('Update Location'),
                                                                'add_new_item'      => __('Add New Location'),
                                                                'new_item_name'     => __('New Location Name')  ), 
                                'public'            => true,
                                'show_ui'           => true,
                                    "rewrite"           => true )
                        );

The pages codes can also be found at:

http://codepad.viper-7.com/4Wkgg3

http://codepad.viper-7.com/n2hZRI

I wasn't able to fix the issue in the end, but instead of using the theme options file to dynamically select the category I want, I used an override code to directly (statically) select the category I wanted and it worked.

share|improve this question
    
What does $cats_array = get_categories('hide_empty=0&taxonomy=location'); return? –  Chip Bennett Aug 29 '12 at 13:58
    
Nothing. the dropdown is blank when I apply that. –  Claudio Delgado Aug 29 '12 at 14:06
    
"Nothing". No. It returns something. What is that something? Is it an empty array? Is it null? What? –  Chip Bennett Aug 29 '12 at 14:09
    
<select id="elegantestate_feat_cat" name="elegantestate_feat_cat"> <option></option> </select> –  Claudio Delgado Aug 29 '12 at 14:10
    
That's not what I'm asking. I'm asking what your get_categories() call returns; not what is ultimately output in your markup. Do a var_dump( $cats_array ). –  Chip Bennett Aug 29 '12 at 14:15
show 2 more comments

3 Answers

Use :

$terms = get_terms( 'location', 'hide_empty=0' );
if(is_wp_error($terms)){
    // error occurred
} else if (empty($terms )){
    // no terms were found
} else {
    // process terms
    foreach($terms as $term){
        // process this individual $term
    }
}

I'd advise you use the generic taxonomy & term functions everywhere, and avoid the special case category and tag functions where possible.

share|improve this answer
add comment

From your comment:

This should be what you're looking for: object(WP_Error)#337 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } }

The error message reveals the problem: "Invalid taxonomy"

You're not passing a valid taxonomy to get_categories().

share|improve this answer
    
but the same very exact call works on other pages. wp_dropdown_categories('orderby=name&show_count=0&selected=0&hierarchical=1&hide‌​_empty=0&depth=1&show_option_all=All&name=locadu&id=locadu&taxonomy=location&excl‌​ude=1061'); works exactly like it should yet the same taxonomy doesn't work in get_categories(). –  Claudio Delgado Aug 29 '12 at 14:33
    
What does get_terms( 'location' ) return? –  Chip Bennett Aug 29 '12 at 14:46
    
object(WP_Error)#517 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } –  Claudio Delgado Aug 29 '12 at 14:48
    
And what does your wp_dropdown_categories() on the same page return? –  Chip Bennett Aug 29 '12 at 14:48
    
Actually, I think you're a genius. Because although I didn't find how to fix it yet, I just found out the wp_dropdown_categories() code only works well in my theme's header file. in the same file that I'm editing right now, it returns with errors as well! So somehow the taxonomy that works when used anywhere in the theme's widget, index, header etc. doesn't work in the file I'm editing which is the theme's options file. –  Claudio Delgado Aug 29 '12 at 14:51
show 10 more comments
up vote 0 down vote accepted

I wasn't able to fix the issue in the end, but instead of using the theme options file to dynamically select the category I want, I used an override code to directly (statically) select the category I wanted

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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