提问者:小点点

单个自定义帖子类型模板(使用木材/树枝的WordPress)不工作


我一直无法使自定义帖子类型的单个细枝模板正常工作。

我的发展。twig页面正确列出了自定义帖子类型开发中的帖子。

但是,单模板默认为单模板。细枝,而不是单一的发展。细枝在单一开发中。我有:

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

就像其他人建议我重新保存permalinks一样,但那没有帮助。

在木材功能方面。我有:

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

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


共3个答案

匿名用户

您的post_类型称为developmentS(复数),因此您的单个文件应命名为singledevelopments。php

匿名用户

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

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

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

您可以在页面底部找到一个示例。您可以为自定义帖子类型创建一个单独的single-developments.php在那里您可以简单地使用wp_query来获取单个帖子的数据。

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

匿名用户

我误解了关于复数和单数的命名惯例。事实上,模板需要single-developments.twig,而不是像我想的那样使用单个模板的singular_name。

我也不需要额外的单个development/s.php。这段代码是单条的。php按预期工作:

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