• 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 this:

    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
    /*
    Single Post Template: Film Overview
    Description: This part is optional, but helpful for describing the Post Template
    */
    ?>
    
    <?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;
    ?>

  • The topic ‘Unable to load custom post template for custom post type’ is closed to new replies.