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

问题描述 投票:2回答:2

使用Ghost博客routes.yaml文件,可以使用集合块来创建由某些标记和/或其他数据构成的自定义集合。您还可以告诉此集合使用自定义主题模板,请参阅:

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

例如:

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),但我还没有找到解决方案。

虽然我最初尝试使用Google搜索解决方案是空的,但这是我在此问题上看到的最佳参考:

  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
ghost-blog
2个回答
0
投票

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

一般做以下事项:

  1. 创建页面示例(使用slug示例)并填充所需的元数据标题和说明
  2. 在routes.yaml中更改您的集合定义/ example / add following: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}}之外,我想你可以使用here定义的所有元数据字段。


0
投票

我找到了一种解决方法。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.