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.

Hi when i try to get_terms(); in theme options via this code

$catalogs_terms = get_terms( 'catalogs' );
   $mycatalogs = array( -1 => 'Select a catalog' );
   if ( $catalogs_terms ) {
      foreach ( $catalogs_terms as $catalog_term ) {
         $mycatalogs[$catalog_term->term_id] = $catalog_term->name;
      }
   }

return empty but this code is working fine every where in pages etc. when i try to print_r( $catalogs_terms ) output i am getting errors

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

i don't understand where am i wrong? my function for register taxonomy

    add_action( 'init', 'my_taxonomies', 0 );

function my_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name' => _x( 'Catalogs', 'taxonomy general name' ),
        'singular_name' => _x( 'Catalog', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Catalogs', 'mytextdomain' ),
        'all_items' => __( 'All Catalogs', 'mytextdomain' ),
        'parent_item' => __( 'Parent Catalog', 'mytextdomain' ),
        'parent_item_colon' => __( 'Parent Catalog:', 'mytextdomain' ),
        'edit_item' => __( 'Edit Catalog', 'mytextdomain' ), 
        'update_item' => __( 'Update Catalog', 'mytextdomain' ),
        'add_new_item' => __( 'Add New Catalog', 'mytextdomain' ),
        'new_item_name' => __( 'New Catalog Name', 'mytextdomain' ),
        'menu_name' => __( 'Catalogs', 'mytextdomain' ),
    );  

    // register catalogs hierarchical (like categories)
    register_taxonomy( 'catalogs',
        array( 'news' ),
        array( 'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'public' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'catalogs' )
        )
    );
}
share|improve this question
    
At a guess you're calling that code before the taxonomy is registered, which is why you get an error object back.. Where specifically are you calling get_terms? (inside a function? Yes, which hook is it attached to?) –  t31os Mar 30 '11 at 11:31
    
in my function.php i have registerd my post type and then taxonomy and below is this code for get terms without function and hook with this code i am able to get categories fine –  Anjum Mar 30 '11 at 11:37
    
Can you put a copy of your functions file into a pastebin or pastie so i can see how it looks in the file? (i can't see the issue offhand, so there must be something missing from what's posted). –  t31os Mar 30 '11 at 11:43
    
@t31os checkout my code here pastie.org/1734827 –  Anjum Mar 30 '11 at 12:07
add comment

1 Answer 1

up vote 4 down vote accepted

As i was mentioning before, it's a case of your term fetching occuring before the taxonomy has been registered.

The init action occurs after the theme's functions file has been included, so if you're looking for terms directly in the functions file, you're doing so before they've actually been registered.

Here's a portion of the code from wp-settings.php that includes the theme functions and does the init action.

// Load the functions for the active theme, for both parent and child theme if applicable.
if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
    include( STYLESHEETPATH . '/functions.php' );
if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
    include( TEMPLATEPATH . '/functions.php' );

do_action( 'after_setup_theme' );

// Load any template functions the theme supports.
require_if_theme_supports( 'post-thumbnails', ABSPATH . WPINC . '/post-thumbnail-template.php' );

register_shutdown_function( 'shutdown_action_hook' );

// Set up current user.
$wp->init();

/**
 * Most of WP is loaded at this stage, and the user is authenticated. WP continues
 * to load on the init hook that follows (e.g. widgets), and many plugins instantiate
 * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
 *
 * If you wish to plug an action once WP is loaded, use the wp_loaded hook below.
 */
do_action( 'init' );

As you can see the init action doesn't fire until after the theme functions file is included, therefore any term retrieval must occur after init. I can't really advise you any further though because you've only shown me a portion of your code, so i've not much of an idea of the context you're trying to use the term function in, but it certainly can't be called directly in the functions file(outside a callback hooked onto a specific action/filter because the code will run to soon).

Hopefully the above illustrates the problem enough for you.. :)

Additional note:
This function is missing a var from the global statement(you'd see PHP notices if you had debug turned on).

function news_updated_messages( $messages ) {
    global $post;

That should read..

function news_updated_messages( $messages ) {
    global $post, $post_ID;

.. because code inside that function references that var, but the variable doesn't have scope inside the function, my above suggested change will fix that.

Follow-up #1

When creating a plugin or theme page, you first have to setup/register that page, this is typically done like so..

add_action('admin_menu', 'my_theme_menu');

function my_theme_menu() {
    add_theme_page( 'Theme Settings', 'Theme Settings', 'manage_options', 'my-unique-identifier', 'my_theme_settings' );
}
function my_theme_settings() {

    // Code to display/handle theme options would be here
    // You get_terms() call should work inside this function just fine

}

If the theme page is created differently, specific to the premium theme you're using then i can't really help as they tend to use a framework that is entirely different to regular WordPress themes.

share|improve this answer
    
@t31os is there a way to get terms from wpdb? or some other –  Anjum Mar 30 '11 at 13:07
    
Why? Is there a specific reason you must fetch them directly in the functions file? I can't think why you'd need to. –  t31os Mar 30 '11 at 13:10
    
i want to display terms in theme admen options panel for select a term to show news within selected term on a page with get_option –  Anjum Mar 30 '11 at 13:20
    
Ok but your theme options page is a registered page right? (ie. you added it via one of the add_%type%_page functions inside a function hooked onto admin_menu) That registered page has a callback function, if you do the term fetching inside that function it should be fine. –  t31os Mar 30 '11 at 13:26
    
i can,t understand how to do this please any advice Thanks –  Anjum Mar 30 '11 at 13:33
show 9 more comments

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.