WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Taxonomies (7 posts)

  1. tolga_2010
    Member
    Posted 3 years ago #

    Hi,

    I want to get taxonomies of a custom post type in my wp options page. I've used following codes to register taxonomies and posts. But can't get them with the other code below in my options page on wp-admin.

    function portfolio_items()
    {
      $labels = array(
        'name' => _x('Books', 'post type general name'),
        'singular_name' => _x('Book', 'post type singular name'),
        'add_new' => _x('Add New', 'book'),
        'add_new_item' => __('Add New Book'),
        'edit_item' => __('Edit Book'),
        'new_item' => __('New Book'),
        'view_item' => __('View Book'),
        'search_items' => __('Search Books'),
        'not_found' =>  __('No books found'),
        'not_found_in_trash' => __('No books found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','excerpt','comments')
      );
      register_post_type('book',$args);
    }
    
    //hook into the init action and call create_book_taxonomies when it fires
    
    //create two taxonomies, genres and writers for the post type "book"
    function create_book_taxonomies()
    {
      // Add new taxonomy, make it hierarchical (like categories)
      $labels = array(
        'name' => _x( 'Genres', 'taxonomy general name' ),
        'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Genres' ),
        'all_items' => __( 'All Genres' ),
        'parent_item' => __( 'Parent Genre' ),
        'parent_item_colon' => __( 'Parent Genre:' ),
        'edit_item' => __( 'Edit Genre' ),
        'update_item' => __( 'Update Genre' ),
        'add_new_item' => __( 'Add New Genre' ),
        'new_item_name' => __( 'New Genre Name' ),
      ); 	
    
      register_taxonomy('genre',array('book'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'genre' ),
      ));
    }

    I've add some taxonomies into genre and used this one to get in wp admin

    $portfolio_categories =get_terms('genre', 'orderby=count&hide_empty=0');
    
           foreach ($portfolio_categories as $portf_tax_list ) {
    	      echo $portf_tax_list->ID;
                  echo $portf_tax_list->name;
           }

    this code give blank values.

    can anyone help me please.

    thanks in advance

    by the way this code listing the taxonomies correctly in single.php

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    Where are you running this code?

    $portfolio_categories =get_terms('genre', 'orderby=count&hide_empty=0');
    
           foreach ($portfolio_categories as $portf_tax_list ) {
    	      echo $portf_tax_list->ID;
                  echo $portf_tax_list->name;
           }

    NOTE: You've referenced an invalid object var here.

    echo $portf_tax_list->ID;

    Should be.

    echo $portf_tax_list->term_id;
  3. AlexLakes
    Member
    Posted 3 years ago #

    Have you tried the CMS Press plugin yet? It'll do this for you.

  4. tolga_2010
    Member
    Posted 3 years ago #

    Hi, Thanks for the answers!

    I've moved the code after the create_book_taxonomies function and it's working now.

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    I half wondered whether you were trying to output the code inside one of the functions, but never got the chance to ask(you figured it out), as yes that most certainly wouldn't have worked... :)

  6. tolga_2010
    Member
    Posted 3 years ago #

    thank you t31os_

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're welcome.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic