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.

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 ?

share|improve this question

1 Answer 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' )
share|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 at 7:22

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.