0

I have similar problem to this one: Custom Post Type Template - Archive

I have a WordPress custom post type called coupon. I try to assign the template to it by using archive-coupon.php, but it goes directly to home.php.

I also have a custom taxonomy of this CPT called tags with the rewrite rule => "tag" so when I go to a URL like this - http://mysite.com/tag/3d I want to see a custom template for my Custom Post Type or general for taxonomies.

I have also tried taxonomy.php template but it still ignores it. What could be the problem?

Update -

I don't know what really happened. But before that I had template called taxonomy-store.php. So, when I renamed it to another name, taxonomy tag started using taxonomy.php template file. And still archive-coupon.php doesn't work.

2
  • 2
    Can you post the code from your functions.php that declares the CPT? Commented Jun 21, 2012 at 18:49
  • Also, have you re-saved permalinks after declaring the CPT? Commented Feb 1, 2014 at 16:20

3 Answers 3

2

I think you should add support for custom taxonomy types and name the file taxonomy-cpt-type.php where cpt is the name of your Custom Post Type.

Your CPT archive file should be named archive-cpt.php where cpt is the name of your Custom Post Type.

Here's all the code which has been tested.

Note: You shouldn't be using tag or tags as the name of your custom taxonomy type as it will conflict with existing core functions.

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

register_post_type( 'cpt',
    array(
        'labels' => array(
            'name'          => __( 'CPT', 'theme' ),
            'singular_name' => __( 'CPT', 'theme' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => true,
        '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' ),

    ));

}

And this for registering a Taxonomy Type page where you can create unlimited tax types.

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

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

}

Replace all instances of cpt with the name of your custom post type then re-save your Permalink settings.

Otherwise, it may be a problem with the code in your taxonomy-cpt-type.php file.

1

i believe tag is a reserved taxonomy rewrite name on wordpress to be used on default post_tag taxonomy. That might cause the issue. Rename the custom taxonomy to something like coupon-tag.

BTW: did you try flushing the rewrites by visiting permalink page from settings tab?

2
  • yes, I have updated the permalinks settings. Still didn't work. I also used another slug for rewrite, and got the same issue
    – Godforever
    Commented Jun 21, 2012 at 20:58
  • @Godforever: okey, you will need to show your code to get further help.
    – Sisir
    Commented Jun 23, 2012 at 7:46
0

First off, let's make sure you understand the template hierarchy. For a post type named coupon with 'has_archive' => true and a taxonomy named coupon-tag (following @Sisir's advice), both set to public,

  • /coupon/ will use archive-coupon.php, archive.php, or index.php, whichever is found in that order
  • /coupon-tag/foo/ will use taxonomy-coupon-tag-foo.php, taxonomy-coupon-tag.php, taxonomy.php, archive.php, or index.php, whichever is found first in that order. Note that archive-coupon.php is not in this list (that's a common mistake).
  • home.php will only be used for the homepage

Next, tag is a special word and you will encounter rewrite conflicts using that as your slug. You should use something else.

Lastly, flushing your rewrites is very important here, and you need to remember to do it after any changes to any of the relevant code (any changes to the CPT or taxonomy). The easiest way to do so is to go to Settings → Permalinks and click "Save Changes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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