0

I've created a custom post type, 'film' am having trouble getting posts of this type to use the custom post template I've created in my theme directory.

I created the 'film' post type in functions.php by doing the following:

add_action('init', 'create_films_post_type');
function create_films_post_type() {
     $film_labels = array(
         'name' => 'Films',
         'singular_name' => 'Film',
         'add_new' => 'Add Film',
         'add_new_item' => 'Add Film',
         'edit' => 'Edit Film',
         'edit_item' => 'Edit Film',
         'new_item' => 'New Film',
         'view' => 'View Film',
         'view_item' => 'View Film',
         'search_items' => 'Search Film',
         'not_found' => 'No films Found',
         'not_found_in_trash' => 'No films found in trash'
    );
    $args = array(
        'labels' => $film_labels,
        'description' => 'Films custom post type',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'film', 'with_front' => false),
        'capability_type' => 'post',
        'menu_position' => 4,
        'exclude_from_search' => false,
        'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'page-attributes', 'custom-fields'),
    );
    register_post_type('film', $args);
    flush_rewrite_rules();
}

And then copied my single.php file and renamed it to single-film.php. It has the following:

<?php get_header(); ?>
<div class="main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        Film post type is working!
    <?php endwhile; ?>
    <?php endif; ?>
</div> <!-- end #main -->
<?php get_footer(); ?>`

But no matter what I do, film posts continue to use single.php, and not single-film.php. I also tried adding this filter to my functions.php, but it did nothing more than break the site:

add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
    global $post;

    if ($post->post_type == 'film') {
        $single_template = get_template_directory_uri() . '/single-film.php';
    }
    return $single_template;
}

I can't determine if I've not set up the film post type correctly, if the custom post template needs something it doesn't have, or another root cause I'm not aware of. Any insights would be greatly appreciated.

Update: When I add the following to my single.php, it displays the film's title and content in any posts using single.php...so it's pretty clear that post type 'film' is alive and breathing, but for some reason isn't using custom post template single-film.php:

<?php
$args = array( 'post_type' => 'film', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile;
?>
  • first remove flush_rewrite_rules() – Kika Sep 30 '15 at 18:42
  • I initially added this because I was getting a 404 error when trying to view one of the custom posts, even after viewing the Permalinks page. I just removed it and while there's no 404, the custom post is still using single.php. – andrw22 Sep 30 '15 at 19:02
  • sorry but you are sure you use single-film.php not single-Film.php or single_film.php... double check spelling & CAPS – Kika Sep 30 '15 at 19:22
  • In a fresh install of WordPress your code works as expected. Is it possible you have any plugins installed that could be creating the template routing issue? Any overrides in your existing theme perhaps? – dswebsme Sep 30 '15 at 19:52
  • 1
    Embarrassingly enough, the file extension for film-single was .html, not .php. – andrw22 Sep 30 '15 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.