0

I've made a custom post type book and a custom taxonomy book_category. I've been trying to create an archive page where the books belonging to a particular category can be viewed. So far I've only been able to get an archive page of all the books by creating an archive-book.php file to customize the view. What should be the next step from here ?

My preferred url should be of the form: mysite.com/books/[slug of book_category]

My understanding is that some kind of URL rewrite rule has to be in place, but I'm not sure how to do that. Any help would appreciated, thanks.

The custom post type was registered in the following way:

register_post_type( 'book',
    array(
        'labels' => array(
            'name' => __( 'Books' ),
            'singular_name' => __( 'Book' ),
            'menu_name' => __('Books'),
            'all_items' => __('All books')
        ),
    'public' => true,
    'has_archive' => true,
    'taxonomies' => array('book_category', 'book_author'),
    'supports' => array( 'title', 'comments', 'excerpt', 'custom-fields', 'thumbnail', 'editor' ),
    'rewrite' => true,
    )
);

To register the custom taxonomy :

register_taxonomy(
    'book_category',
    'book',
    array(
        'label' => __( 'Book Categries' ),
        'labels' => array(
            'name' => 'Book Categries',
            'singular_name' => 'Book Categry',
            ),
        'public' => true,
        'show_ui' => true,
        'rewrite' => array( 'slug' => 'book_category' ),
        'capabilities' => array(
            'assign_terms' => 'edit_guides',
            'edit_terms' => 'publish_guides'
        ),
        'meta_box_cb' => 'show_book_category_ui'
    )
);

Edit:

I managed to make the url change by following Milo's advice. But the taxonomy-book_category.php then becomes useless. Trying to rename it to taxonomy-books.php did not work. What should be the name of this file now ?

1

To achieve the URL mysite.com/books/[slug of book_category], in your taxonomy registration code, change:

'rewrite' => array( 'slug' => 'book_category' )

to:

'rewrite' => array( 'slug' => 'books' )
| improve this answer | |
  • Hi Milo, I managed to change the URL by following your advice. But now the taxonomy-book_category.php is not being used anymore. I tried to rename it to taxonomy-books.php, that did not work either. What should I do now ? – Raiyan Jan 22 '14 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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