categories - How to determ a custom post type url?

admin2025-01-07  3

For example i added a new post type

add_action('init','register_book_type');
function register_book_type() {
    $labels = array(
        'name' => 'Book',
        'singular_name' => 'Book',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New',
        'edit_item' => 'Edit Book',
        'new_item' => 'New Book',
        'all_items' => 'All Book',
        'search_items' => 'Find Book',
        'not_found' =>  'Book not found.',
        'not_found_in_trash' => 'There are no books in the trash.',
        'menu_name' => 'Books'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'booklist'), 
        'menu_icon' => 'dashicons-admin-post',
        'menu_position' => 2,
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields')

    );


    register_post_type( 'ptbook', $args );
}

After I created a few post without any category.

And what i have in final :

www.domain/booklist (show me all created custom posts)

www.domain/booklist/post-name (show me a post with a data)

Now I want to know how to determ the "category" of booklist

For example for normal categories works this code

if(is_category("cat name here")) { ... } 

But for custom post is not work and not work esle anything.. i tried to few code like is_terms, is_tax no one detect boooklist

For example i added a new post type

add_action('init','register_book_type');
function register_book_type() {
    $labels = array(
        'name' => 'Book',
        'singular_name' => 'Book',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New',
        'edit_item' => 'Edit Book',
        'new_item' => 'New Book',
        'all_items' => 'All Book',
        'search_items' => 'Find Book',
        'not_found' =>  'Book not found.',
        'not_found_in_trash' => 'There are no books in the trash.',
        'menu_name' => 'Books'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'booklist'), 
        'menu_icon' => 'dashicons-admin-post',
        'menu_position' => 2,
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields')

    );


    register_post_type( 'ptbook', $args );
}

After I created a few post without any category.

And what i have in final :

www.domain.com/booklist (show me all created custom posts)

www.domain.com/booklist/post-name (show me a post with a data)

Now I want to know how to determ the "category" of booklist

For example for normal categories works this code

if(is_category("cat name here")) { ... } 

But for custom post is not work and not work esle anything.. i tried to few code like is_terms, is_tax no one detect boooklist

Share Improve this question asked Dec 28, 2024 at 16:13 webstackoverloadwebstackoverload 1417 bronze badges 1
  • Give complete details before asking a question. Specify that first whether you have created custom category for custom post type or not. Your question should be clear and has all the details. Add taxonomy creation code if you have created it. – Ashok Dhaduk Commented Dec 29, 2024 at 4:31
Add a comment  | 

1 Answer 1

Reset to default 3

You need to register a custom taxonomy for you custom post type.

For example:

add_action('init', 'register_book_taxonomy');
function register_book_taxonomy() {
    $labels = array(
        'name' => 'Book Categories',
        'singular_name' => 'Book Category',
        'search_items' => 'Search Book Categories',
        'all_items' => 'All Book Categories',
        'parent_item' => 'Parent Book Category',
        'parent_item_colon' => 'Parent Book Category:',
        'edit_item' => 'Edit Book Category',
        'update_item' => 'Update Book Category',
        'add_new_item' => 'Add New Book Category',
        'new_item_name' => 'New Book Category Name',
        'menu_name' => 'Book Categories',
    );

    $args = array(
        'hierarchical' => true, // Works like categories
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'book-category'), // URL slug for the taxonomy
    );

    register_taxonomy('book_category', array('ptbook'), $args);
 }

Then you can create categories, and assign posts (custom posts) to it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255397a277.html

最新回复(0)