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 have 3 custom post types - Events, Music and Video. I have custom article pages for these 3 post types as having them on a singular archive page would be bloated. So i have:

  1. archive-events.php
  2. archive-music.php
  3. archive-video.php

When a taxonomy is clicked (Tag or Category) it defaults to the standard archive page. To rectify this I have set up custom taxonomy templates ie. taxonomy-tagevents.php. This work perfectly. My question is though, I presume there is a better way to do this so I don't have 3 copies of taxonomy-(custom-taxonomy). Is there a way to direct the taxonomy to the custom post type archive page?

UPDATE

<?php 

// Register Custom Post Type
function custom_post_type_music() {

    $labels = array(
        'name'                => _x( 'Music', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Music', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Music', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Music:', 'text_domain' ),
        'all_items'           => __( 'All Music', 'text_domain' ),
        'view_item'           => __( 'View Music', 'text_domain' ),
        'add_new_item'        => __( 'Add New Music Tracks', 'text_domain' ),
        'add_new'             => __( 'New Music Tracks', 'text_domain' ),
        'edit_item'           => __( 'Edit Music', 'text_domain' ),
        'update_item'         => __( 'Update Music', 'text_domain' ),
        'search_items'        => __( 'Search Music', 'text_domain' ),
        'not_found'           => __( 'No Music found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No Music found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'Music', 'text_domain' ),
        'description'         => __( 'Music information pages', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => '',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'Music', $args );

// Initialize Taxonomy Labels
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name', 'text_domain' ),
        'singular_name' => _x( 'Category', 'taxonomy singular name', 'text_domain' ),
        'search_items' =>  __( 'Search Types', 'text_domain' ),
        'all_items' => __( 'All Categories', 'text_domain' ),
        'parent_item' => __( 'Parent Category', 'text_domain' ),
        'parent_item_colon' => __( 'Parent Category:', 'text_domain' ),
        'edit_item' => __( 'Edit Categories', 'text_domain' ),
        'update_item' => __( 'Update Category', 'text_domain' ),
        'add_new_item' => __( 'Add New Category', 'text_domain' ),
        'new_item_name' => __( 'New Category', 'text_domain' ),
    );

    // Register Custom Taxonomy
    register_taxonomy('tagmusic',array('music'), array(
        'hierarchical' => true, // define whether to use a system like tags or categories
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'cat-music' ),
    ));

}

// Hook into the 'init' action
add_action( 'init', 'custom_post_type_music', 0 );
share|improve this question
    
As-written, your question is a bit confusing. Are you sure you're following the WordPress Template Hierarchy properly? The correct template file to use for a single CPT post is single-{post-type}.php, not article-{post-type}.php. And WordPress doesn't support taxonomy-{post-type}.php; rather, it supports taxonomy-{taxonomy}.php and taxonomy-{taxonomy}-{term}.php. –  Chip Bennett Jan 8 at 17:38
    
Hi @ChipBennett . I may of not made myself clear. This is to display in the article page. Not in the single pages. I am using 'article-slug' –  user2960468 Jan 8 at 17:45
    
The taxonomy-[taxonomy] is displaying my custom taxonomy of my custom posts –  user2960468 Jan 8 at 17:46
    
I'm not sure why or how you have three different taxonomy-{taxonomy}.php template files. Please add your relevant code, in context - including your register_post_type() and register_taxonomy() calls. Also: by "article page", do you mean custom page template? –  Chip Bennett Jan 8 at 18:02
    
@ChipBennett Updated with code above. Opps sorry, I meant archive! My mistake (Been a long day). I need different taxonomies because I have various templates specific to the custom posts so they are not compatible with general categories. –  user2960468 Jan 8 at 18:52

2 Answers 2

up vote 0 down vote accepted

If I'm understanding your question correctly, you want to avoid code duplication among the three CPT archive and three taxonomy template files?

If the point is primarily one of avoidance of code duplication (i.e. DRY - Don't Repeat Yourself), I would recommend creating template part files for the loop content for each CPT:

  • content-{cpt1}.php
  • content-{cpt2}.php
  • content-{cpt3}.php

You'll still need the template files for the CPT archives and the taxonomy indexes:

  • archive-{cpt1}.php
  • taxonomy-{taxonomy1}.php
  • archive-{cpt2}.php
  • taxonomy-{taxonomy2}.php
  • archive-{cpt2}.php
  • taxonomy-{taxonomy2}.php

But then inside each of those files, just replace the loop markup with the appropriate template-part file. For example, archive-{cpt1}.php and taxonomy-{taxonomy1}.php might look like this:

<?php
get_header();

get_template_part( 'content', 'cpt1' );

get_footer();
?>

That's the most straight-forward method. However, there is a method that uses fewer template and template-part files: filtering template_include (or hooking into template_redirect) to tell WordPress which template file to use.

To start, create three template files, one for each CPT/taxonomy:

  • template-cpt1.php
  • template-cpt2.php
  • template-cpt3.php

(Note that these are full template files - header, content, and footer - and not merely template-part files.)

Then, just tell WordPress when to use them.

For example:

function wpse129011_include_cpt_templates( $template ) {
    // CPT 1 archive index or CPT 1 taxonomy index
    if ( is_post_type_archive( 'cpt1' || is_tax( 'taxonomy1' ) {
        return get_template_directory() . '/template-cpt1.php';
    }     
    // CPT 2 archive index or CPT 2 taxonomy index
    else if ( is_post_type_archive( 'cpt2' || is_tax( 'taxonomy2' ) {
        return get_template_directory() . '/template-cpt2.php';
    }     
    // CPT 3 archive index or CPT 3 taxonomy index
    else if ( is_post_type_archive( 'cpt3' || is_tax( 'taxonomy3' ) {
        return get_template_directory() . '/template-cpt3.php';
    }
    return $template;
}
add_filter( 'template_include', 'wpse129011_include_cpt_templates' );
share|improve this answer
    
Thanks Chip. Thanks for the in-depth answer. I already do the first option using template files. I want to reduce the number of templates so I think I will give the second method a go. Thanks again, you have been very patient! –  user2960468 Jan 8 at 19:20

You can add support for creating custom taxonomy types and include one template which works for an unlimited amount of taxonomies.

Here's the code i use to create a CPT which also supports the creation of unlimited custom taxonomy types.

Here's the full code which also includes code for creating a Custom Post Type in any theme. http://wordpress.stackexchange.com/a/128544/9884

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

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

}

You can use this code with 1 taxonomy-cpt-type.php template, and you only need one.

share|improve this answer

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.