mkafiyan
mkafiyan

Reputation: 954

WordPress taxonomy doesn't work for my custom post type

I try to create custom post with a taxonomy in WordPress for my theme, my custom post works fine but my taxonomy doesn't show in admin wordpress panel.

I write this part of code in other page named library.inc.php

and I include it in function.php

I cant find where exactly is problem?

here is my code

function p2p2_register_book(){
    $labels = array(
        'name'               => _x( 'کتابخانه', 'books'),
        'singular_name'      => _x( 'کتاب', 'book' ),
        'add_new'            => _x( 'افزودن کتاب', '' ),
        'add_new_item'       => __( 'افزودن کتاب جدید' ),
        'edit_item'          => __( 'ویرایش کتاب' ),
        'new_item'           => __( 'کتاب جدید' ),
        'all_items'          => __( 'همه کتاب ها' ),
        'view_item'          => 'View Book',
        'search_items'       => __( 'جست و جو کتاب' ),
        'not_found'          => _( 'کتاب یافت نشد' ),
        'not_found_in_trash' => __( 'کتاب در زباله دان یافت نشد' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'کتابخانه'
    );
    $args=array(
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'menu_position'      => 2,
            'rewrite'            => array( 'slug' => 'book' ),
            'capability_type'    => 'page',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array( 'title', 'editor', 'thumbnail', 'comments' ,'custom-fields'),
            // 'taxonomies'  => array( 'Books_category','post_tag' ),

        );
    register_post_type('Book',$args);

}

    add_action('init', 'p2p2_register_book');

and here is taxonomy part

            function wp_library_post_taxonomy() {
            register_taxonomy(
                'Books_category',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
                'Book',                  //post type name
            array(
            'hierarchical'          => true,
            'label'                         => 'دسته بندی کتاب',  //Display name
            'query_var'             => true,
            'show_admin_column'             => true,
            'rewrite'                       => array(
                'slug'                  => 'Books-category', // This controls the base slug that will display before each term
                'with_front'    => true // Don't display the category base before
                )
            )
    );
}
        add_action( 'init', 'wp_library_post_taxonomy');

        include_once( 'mixitup-plug.php' );
        add_theme_support('post_thumbnails');
        add_image_size('Books_small_image',150,200,true);

Upvotes: 1

Views: 889

Answers (2)

mkafiyan
mkafiyan

Reputation: 954

here is the answer I spend near 6 hours to find the problem but no result so I rewrite the taxonomy part with exact name and ..... problem solved. I don't know exactly what's wrong with WordPress. really 6 hours for nothing?

Upvotes: 1

Sujan Shrestha
Sujan Shrestha

Reputation: 1040

use following code to register custom taxonomy it will definetly work. :)

//hook into the init action and call create_book_taxonomies when it fires
    add_action( 'init', 'wp_library_post_taxonomy', 0 );

//create a custom taxonomy name it topics for your posts

function wp_library_post_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

    $labels = array(
        'name' => _x( 'Taxonomy Names', 'taxonomy general name' ),
        'singular_name' => _x( 'Taxonomy Name', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Taxonomy Names' ),
        'all_items' => __( 'All Taxonomy Names' ),
        'parent_item' => __( 'Parent Taxonomy Names' ),
        'parent_item_colon' => __( 'Parent Taxonomy Name:' ),
        'edit_item' => __( 'Edit Taxonomy Name' ),
        'update_item' => __( 'Update Taxonomy Name' ),
        'add_new_item' => __( 'Add New Taxonomy Name' ),
        'new_item_name' => __( 'New Granite Taxonomy Name' ),
        'menu_name' => __( 'Taxonomy Name' ),
    );

// Now register the taxonomy

    register_taxonomy('Taxonomy Name',array('custom_post_type name'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'Taxonomy Name' ),
    ));

}

Upvotes: 0

Related Questions