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'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;
}
?>
share|improve this question
    
Just to confirm, it sounds like you want your archive-photos.php file to be used on the homepage, and that's what isn't working, is that right? If you go to yoursite.com/?post_type=photos does it render archive.php or archive-photos.php? –  Matthew Boynes Feb 23 '13 at 20:35
    
No, I simply want the archive-photos.php to be used when viewing the archive page for that specific custom post type. Now it seems that archive.php is used. –  Gab Feb 23 '13 at 20:36
    
Just want to make sure this point is clear: when you go to yoursite.com/?post_type=photos it renders archive.php. Is that correct? –  Matthew Boynes Feb 23 '13 at 20:41
    
Nope! It does render archive-photos.php. So maybe I'm simply mixed up with the urls. OK if you go to gablabelle.com/?post_type=photos&genre=boudoir it works but when I go to gablabelle.com/photos/boudoir it doesn't, it loads archive.php. –  Gab Feb 23 '13 at 20:47
1  
Ah, the genre is a key detail. That's a taxonomy archive not a custom post type archive, so WP is going to look for a taxonomy template. Rename (or copy) your archive-photos.php to taxonomy-genre.php and I bet you'll see what you expect. –  Matthew Boynes Feb 23 '13 at 20:51

1 Answer 1

up vote 3 down vote accepted

as determined in the comments, the page URI is ?post_type=photos&genre=boudoir...

The reason why "archive-{$post_type}.php" isn't used in this case is because the page is a taxonomy archive and not a custom post type archive. WordPress is looking for taxonomy-genre.php instead of archive-photos.php, and not finding it, is defaulting back to good old archive.php. Here's a wonderful cheat-sheet on the template hierarchy, which is so useful it's probably worth tattooing on your forearm.

share|improve this answer
    
Thanks again Matthew. ;-) –  Gab Feb 23 '13 at 21:12

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.