timber / starter-theme

The "_s" for Timber: a dead-simple theme that you can build anything from
MIT License
818 stars 278 forks source link

Single-customposttype.twig not working #116

Closed frontenddevguy closed 3 years ago

frontenddevguy commented 3 years ago

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?

AshleyRedman commented 3 years ago

It looks like you have registered the post type 'developments', plural and the template you have is singular, 'single-development.php', so updating that to 'single-developments.php should fix it.

frontenddevguy commented 3 years ago

Ah such an obvious thing, I thought I had tried it already. For some reason I was thinking the singular / plural naming was working (using the 'singular_name').

Anyway, this works with single-developments.twig, and also does not require the single-developments.php file either.

I can see this:

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

in single.php is working as expected.