1

I used a custom post type in my website: I added archive-{mytaxonomy}.php and also single-{mytaxonomy}.php.

I also created a separate sidebar for my taxonomy and everything looks fine until I click on one of my categories. Then it loads archive.php.

Should I add any other code to my files?

  • 1
    you appear to be confusing custom post types with custom taxonomies, did you add a post type or a taxonomy? have a look at the template hierarchy and note the differences between the two. – Milo Feb 17 '14 at 18:09
1

Categories and Taxonomies are not exactly the same.

A category is a default taxonomy

You can use categories with custom post types however its better to use custom taxonomies because the truth is, they are an extremely powerful way to group various items in all sorts of ways.

add_action( 'init', 'wpsites_custom_taxonomy_types' );
function wpsites_custom_taxonomy_types() {

register_taxonomy( 'cpt-type', 'cpt',
    array(
        'labels' => array(
            'name'          => _x( 'Types', 'taxonomy general name', 'themename' ),
            'add_new_item'  => __( 'Add New CPT Type', 'themename' ),
            'new_item_name' => __( 'New CPT Type', 'themename' ),
        ),
        'exclude_from_search' => true,
        'has_archive'         => true,
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'cpt-type', 'with_front' => false ),
        'show_ui'             => true,
        'show_tagcloud'       => false,
    )
);

}

If you've added code like above which adds the option to create custom taxonomy types, then you'll need to create a file named something like:

taxonomy-cpt-type.php

Where cpt is the name of your custom post type.

You can use the code above in your child themes functions file and replace all instances of cpt and CPT with the name of your custom post type.

You would also need to add this line to the code which registers your custom post type:

'taxonomies'   => array( 'cpt-type' ),

Here's a working example:

add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {

register_post_type( 'cpt',
    array(
        'labels' => array(
            'name'          => __( 'CPT', 'wpsites' ),
            'singular_name' => __( 'CPT', 'wpsites' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => 'dashicons-portfolio',
        'public'       => true,
        'rewrite'      => array( 'slug' => 'cpt', 'with_front' => false ),
        'supports'     => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies'   => array( 'cpt-type' ),

    )
);

}

Again, replace all instances of cpt and CPT with the name of your custom post type.

For custom post type archive pages, use something like this:

archive-cpt.php

For custom post type single pages, use something like this:

single-cpt.php

Again, replace cpt in the file name with the name of your custom post type.

  • categories are a taxonomy, they are functionally identical to any other taxonomy. neither is better or worse, it's simply a matter of what your needs are. – Milo Feb 17 '14 at 23:48
  • Yes however its more flexible to register custom taxonomy types for custom post types than simply using categories. – Brad Dalton Feb 18 '14 at 2:58
1

To get custom post type posts with specific category use custom taxonomy

Register the taxonomy name of the custom post type like categories and then assign a category to each post when you added new post. Here is the example of the code for custom post type news and its taxonomy news_category

  add_action( 'init', 'news_my_taxonomy');
  function news_my_taxonomy(){
 // custom post type taxonomies
    $labels = array(
    'name' => 'Categories',
    'singular_name' => 'Category',
    'add_new' => 'Add Category',
    'add_new_item' => 'Add New Category',
    'all_items' => 'All Categories',
    'edit_item' => 'Edit Item',
    'new_item' => 'New Item',
    'view_item' => 'View Item',
    'update_item' => 'Update Category',
    'search_items' => 'Search Categories',
    'not_found' => 'No record found',
    'not_found_in_trash' => 'No items found in trash',
    'parent_item_colon' => 'Parent Item',
    'menu_name' => 'Categories'
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'news_category'),
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        );
        register_taxonomy('news_category', array('news'), $args);
}

then

create taxonomy template page 'taxonomy-news_category.php'

and add the query to get the posts with this category name

   $cat_name = single_cat_title;
   $args = array( 'category_name' => $cat_name, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'news', 'paged' => $paged);

That's it.

0

If you are using "Custom Post Types UI" plugin to make custom post types make sure you have changed Has Archive option to True, if you made your custom post type manually make sure you have added "'has_archive' => true" to the array.

Then, lets say you added a new custom post type called "Movies", and then you added a new taxonomy linked to it which slug is called "genres" and added a new term to Genres which slug is called "horror",and you wanted to make a specific template for "Horror", then you can make a template which named "taxonomy-{taxonomy}-{term}.php".

In this case the template should be named taxonomy-genres-horror.php

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.