微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单个自定义帖子类型模板WordPress使用Timber / Twig不起作用

如何解决单个自定义帖子类型模板WordPress使用Timber / Twig不起作用

我一直无法使用单个树枝模板来使自定义帖子类型起作用。

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

但是,单个模板认为single.twig,而不是single-development.twig。 在single-development.PHP中,我有

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

像其他人一样,我保留了永久链接,但这没有帮助。

在timber-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_type称为developmentS(复数),因此您的单个文件应命名为single-developments.php

,

注册一个新的自定义帖子类型必须在functions.php中 除了您编写脚本的方式之外,注册帖子类型似乎不是正确的方式。没有名为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'。

我也不需要额外的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 );
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。