【问题标题】:Single custom post type template (WordPress using Timber/Twig) not working单个自定义帖子类型模板(使用 Timber/Twig 的 WordPress)不起作用
【发布时间】:2020-10-13 12:33:00
【问题描述】:

我无法让自定义帖子类型的单个树枝模板正常工作。

我的 development.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);

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

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

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

【问题讨论】:

  • 我看到您将自定义帖子类型注册为developments,它是复数形式,末尾带有“s”。这意味着您必须使用 single-developments.php 而不是 single-development.php 作为文件名。

标签: wordpress twig timber


【解决方案1】:

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

【讨论】:

  • 你先到了 :)
【解决方案2】:

注册一个新的自定义帖子类型必须在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/

【讨论】:

  • timber-functions.php 是从 functions.php 调用的,并且在开发帖子类型注册和从 developments.php 调用的情况下工作,它显示了来自 ACF 的开发和字段列表.看来你对木材不熟悉?据我了解 register_post_types 是木材或木材入门主题的功能
  • 那么我建议你阅读这里的讨论wordpress.stackexchange.com/questions/162065/…
  • 感谢您的尝试,但此评论与此处无关。
【解决方案3】:

我误解了复数和单数的命名约定。 该模板实际上需要是 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 );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2022-09-25
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多