I'm trying to create an archive-photos.php template for my photos custom post type. The problem I have is that when I view the page it is the archive.php template that is used, not the archive-photos.php. I simply want the archive-photos.php to be used when viewing the archive page for that specific custom post type.
What am I missing here?
Please note at the end that I have set the photos custom post type to be the main loop.
Many thanks for your time and help.
<?php
//custom post type
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'photos',
array(
'labels' => array(
'name' => __( 'Photos' ),
'singular_name' => __( 'Photo' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'photo'),
'menu_position' => 5,
'taxonomies' => array('genre'),
'supports' => array('title', 'editor', 'thumbnail', 'post-formats'),
)
);
flush_rewrite_rules( false );
//this enables post formats.
add_theme_support( 'post-formats', array( 'image', 'chat', 'video', 'gallery' ) );
remove_post_type_support( 'post', 'post-formats' );
}
//hook into the init action and call create_photos_taxonomies when it fires
add_action( 'init', 'create_photos_taxonomies', 0 );
//create taxonomy, genres for the post type "photos"
function create_photos_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
register_taxonomy('genre',array('photos'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'photos' ),
));
}
function my_post_class( $classes ) {
global $post;
$terms = wp_get_object_terms( $post->ID, 'genre' );
foreach ( $terms as $genre ) {
$classes[] = $genre->slug;
}
return $classes;
}
add_filter( 'post_class', 'my_post_class' );
//show posts of post type 'photos', 'page' on home page
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
//make photos custom post type the default post type for the main loop
function add_my_custom_post_type( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'photos' ) );
return $query;
}
?>
archive-photos.php
totaxonomy-genre.php
and I bet you'll see what you expect.