单个自定义 post 类型模板(使用 Timber/Twig 的 WordPress)不工作

Single custom post type template (WordPress using Timber/Twig) not working

我一直无法获得自定义 post 类型的单一树枝模板。

我的 developments.twig 页面正确地列出了自定义 post 类型开发中的 post。

不过,单模板默认为single.twig,而不是单模板-development.twig。 在单development.php我有:

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

就像其他人建议的那样,我重新保存了永久链接,但这没有帮助。

在木材中-functions.php我有:

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' ),
        ));
    }

这个结构应该如何工作..我错过了什么吗?

注册新的自定义 post 类型必须在 functions.php 内 除了您编写脚本的方式之外,注册 post 类型的方式似乎也不正确。没有名为 register_post_types() 的函数,它是 register_post_type();

这是开发人员手册,您可以在其中获取自定义 post 类型的详细信息。

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

您可以在页面底部找到示例。您可以为自定义 post 类型创建单个 single-developments.php ,您可以在其中简单地使用 wp_query 获取单个 post.[=12 的数据=]

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

您的 post_type 名为 developmentS(复数),因此您的单个文件应命名为 single-developments.php

我误解了关于复数和单数的命名约定。 该模板实际上需要 single-developments.twig,并且没有像我想的那样将 'singular_name' 用于单个模板。

我也不需要额外的 single-development/s.php。 single.php 中的这段代码按预期工作:

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