如何在我的自定义模板中设置Ghost Blog Custom Routes.yaml Collection Title/



使用Ghost博客routes.yaml文件,可以使用collections块创建由一些标记和/或其他数据组成的自定义集合。你也可以告诉这个集合使用自定义主题模板,请参阅:

  1. https://docs.ghost.org/tutorials/creating-content-collections/
  2. https://docs.ghost.org/concepts/routing/#content-结构

例如:

collections:
/example/:
permalink: /example/{slug}/
controller: channel
filter: tag:example-tag
template:
- example

以上所有作品和我的收藏都正确地使用了我新的example主题文件。

问题是,与标签页(用于example-tag)不同,我的新自定义集合没有一种易于记录的方式来处理标题等。

它不会从用于构建集合的标签中提取标题/元描述(这对于从单个标签构建的集合来说非常好)。为了解决这个问题,我尝试了一些{{#has}}语句,但我不知道自定义路由适合什么上下文。

在上面的routes.yaml示例中,自定义集合的标题最终为"我的网站名称(第1页)",并且没有元描述。

此问题还扩展到Open Graph数据中,该数据列出了相同的标题,但没有对自定义集合的描述。

我的猜测是,可以使用routes.yaml文件附带的数据属性来实现这一点(请参阅:https://docs.ghost.org/concepts/routing/#data)但到目前为止,我还没有找到解决方案。

虽然我最初在谷歌上搜索解决方案的尝试是失败的,但这是我见过的关于这个问题的最佳参考:

  1. https://forum.ghost.org/t/dynamic-routing-page-post-data-context-in-default-hbs-nested-navigation-on-custom-collections/4204
  2. https://github.com/TryGhost/Ghost/issues/10082

我找到了解决问题的方法。

  1. 您可以在Ghost Admin工具中创建一个名为example的页面
  2. 在routes.yaml中自定义路由(而不是集合),如下所示:
routes:
/example/:
controller: channel
filter: tag:example-tag
template: example
data: page.example

page.example将在Ghost中使用此页面的元数据。

这只有在问题中描述的解决方法下才能实现:https://github.com/TryGhost/Ghost/issues/10082

通常执行以下操作:

  1. 创建页面示例(使用slug实例)并填充元数据标题&您想要的描述
  2. 在routes.yaml中更改集合定义/example/添加以下内容:data: page.example将集合根与指定页面链接
  3. 现在,在您的模板定义example.hbs中,您可以使用例如{{#page}} {{content}} {{/page}}标记来插入页面中的内容。您也可以在example.hbs中包含的default.hbs模板中执行此操作。因此,将default.hbs中的<title>{{meta_title}}</title>替换为以下内容:
{{#unless page}}
<title>{{meta_title}}</title>
{{else}}
{{#page}}
<title>{{meta_title}}</title>
<meta name="description" content="{{meta_description}}"/>
{{/page}}
{{/unless}}

这将以一般方式为您的收藏根页面设置特定的标题/描述。可以用类似的方式生成schema.org元数据。不幸的是,Facebook和Twitter的元数据并不是那么简单,因为default.hbs中的{{ghost_head}}标记已经将站点元数据插入到该页面中。最后注意:除了{{meta_title}}{{meta_description}}之外,我想您还可以使用这里定义的所有元数据字段。

default.hbs中,我添加了以下块:

{{{ block "metaTitle"}}}

例如,在post.hbs中,我填充该块如下:

{{!< default}}
<div class="content-area">
<main class="site-main">
{{#post}}
{{#contentFor "metaTitle"}}
<title>{{title}}</title>
{{/contentFor}}
...

对于page.hbs、blog.bbs、author.hbs等其他页面,我也这么做了。我认为这种解决方案更灵活,因为我们对产权价值有更多的控制权。

最新更新