如何在自定义模板中设置 Ghost Blog Custom Routes.yaml 集合标题/元描述?

Nec*_*vil 5 ghost-blog

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

\n\n
    \n
  1. https://docs.ghost.org/tutorials/creating-content-collections/
  2. \n
  3. https://docs.ghost.org/concepts/routing/#content-struct
  4. \n
\n\n

例如:

\n\n
collections:\n  /example/:\n    permalink: /example/{slug}/\n    controller: channel\n    filter: tag:example-tag\n    template:\n      - example\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n

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

\n\n
\n

在上面的routes.yaml示例中,自定义集合的标题最终为“我的站点名称(第1页)”\xe2\x80\x94\xc2\xa0,并且没有元描述。

\n
\n\n

此问题还扩展到开放图谱数据,该数据列出了相同的标题,但没有自定义集合的描述。

\n\n

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

\n\n
\n

虽然我最初尝试在谷歌上搜索解决方案却一无所获,但这是我见过的对该问题的最佳参考:

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

xia*_*oke 5

我找到了一种解决方法。

  1. example您创建一个在 Ghost 管理工具中调用的页面。
  2. 在routes.yaml中自定义路由(而不是集合),如下所示:
routes:
  /example/:
    controller: channel
    filter: tag:example-tag
    template: example
    data: page.example
Run Code Online (Sandbox Code Playgroud)

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


Mar*_*gor 1

这只能通过问题中描述的解决方法来实现:https ://github.com/TryGhost/Ghost/issues/10082

一般做以下几件事:

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

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