1
votes

I've been unable to get the single twig templates for a custom post type to work.

My developments.twig page correctly lists the posts in custom post type developments.

However, the single template defaults to single.twig, rather than single-development.twig. In single-development.php I have:

$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;
Timber::render('single-development.twig', $context);

Like some others suggested I resaved the permalinks but that didn't help.

In timber-functions.php I have:

public function register_post_types() {
        // Use categories and tags with attachments
        register_taxonomy_for_object_type( 'category', 'attachment' );
        register_taxonomy_for_object_type( 'post_tag', 'attachment' );

        register_post_type('developments', array(
            'labels' =>
                array(
                    'name' => __( 'Developments' ),
                    'singular_name' => __( 'development' )
                ),
            'public' => true,
            'supports' => array( 'title', 'author', 'excerpt', 'custom-fields', 'revisions', 'page-attributes' ),
            'show_in_menu' => true,
            'taxonomies'  => array( 'category' ),
        ));
    }

How is this structure supposed to work.. have I missed something?

3
I see that you registered your custom post type as developments, as plural with an "s" at the end. This means that you would have to use single-developments.php instead of single-development.php as your file name.Gchtr

3 Answers

1
votes

Your post_type called developmentS (plural), so your single file should be named single-developments.php

0
votes

Register a new custom post type has to be inside functions.php Besides the way you wrote the script does not seem the right way to register a post type. There are no function named as register_post_types(), it's register_post_type();

Here is the developer handbook where you can get the details of custom post type.

https://developer.wordpress.org/reference/functions/register_post_type/

You can find an example at the bottom of the page. You can create a single for custom post type as single-developments.php where you simply can use wp_query to get the data for single post.

Reference: https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/

0
votes

I misunderstood the naming conventions around plural and singular. The template in fact needed to be single-developments.twig, and was not using the 'singular_name' for single template as I had thought.

I also did not need the extra single-development/s.php. This code in single.php works as expected:

else {
    Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
}