Expression Engine CMS 如何动态填充元标记?

Expression Engine CMS How to populate meta tags dynamically?

我正在尝试将描述和关键字的元标记添加到我的表达式引擎网站。

我的结构是这样的: 我有一个在每个模板中调用的 {top} 代码段

在 head 标签里面我有这个

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{exp:channel:entries}{blog_seo_description}{/exp:channel:entries}">
<meta name="author" content="http://epicsoftware.com" >
<meta name="keywords"  content="{blog_seo_keywords}" />
{if segment_1 == ""}
<title>Epic Software Group, Inc.</title>
{if:else}
{exp:channel:entries channel="main|blog|projects" limit="1" disable="categories|category_fields|custom_fields|member_data|pagination"}
<title>Epic Software Group, Inc. - {title}</title>
{/exp:channel:entries}
{/if}

当我为一个页面编写描述时,它在所有地方都应用了相同的描述,我认为这是因为顶部代码段不知道信息的来源。 另外,我无法在其他频道字段组中创建另一个同名的频道字段

我需要为每个频道创建一个频道字段,并在元标记中显示该频道条目的信息。

表达式引擎版本:2.11.2

使用布局可以更轻松地做到这一点:https://docs.expressionengine.com/v2/templates/layouts.html 基本上,您将拥有一个包含基本模板的包装器模板,并将另一个模板的内容提供给该模板。 这样您只需使用一次 channel:entries 标签即可设置所有数据。 这是我设置变量的基本模板:

{layout="_partials/_wrapper"}
{exp:channel:entries 
 channel="pages" 
 disable="categories|pagination|member_data|relationships"}
{layout:set name="extra_header_content"}<script src="/assets/js/my_extra_script.js">{/layout:set}
{layout:set name="browser_title"}{browser_title}{/layout:set}
{layout:set name="seo_description"}{seo_description}{/layout:set}
{layout:set name="page_title"}{page_title}{/layout:set}
{layout:set name="body_content"}{body_text}{/layout:set}
{/exp:channel:entries}

看到我在第一行嵌入布局模板。 我的包装器模板如下所示:

<!doctype html>
<html class="no-js" lang="nl" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{layout:browser_title}</title>
<meta name='description' content='{layout:seo_description}' />
<meta name="twitter:description" content="{layout:seo_description}" />
<meta property="og:description" content="{layout:seo_description}">
{layout:extra_header_content}
</head>
<body>
<h1>{layout:page_title}</h1>
{layout:body_content}
</body>
</html>

由于您使用的是 EE 2,因此您需要在其自己的字段组中为每个浏览器标题创建字段。这可能很乏味,但是如果您以合乎逻辑的方式命名字段,则可以使用 preload_replace https://docs.expressionengine.com/v2/templates/globals/preload_replacement.html 使您的模板更容易:

假设您有一个名为 news 的频道,将您的字段命名为 "news_browser_title" 并为名为 pages 的频道创建一个名为 "pages_browser_title"

的字段

在您的模板中,您现在可以像这样使用它:

{layout="_partials/_wrapper"}
{preload_replace:channel="pages"}
{exp:channel:entries 
 channel="pages" 
 disable="categories|pagination|member_data|relationships"}
{layout:set name="extra_header_content"}<script src="/assets/js/my_extra_script.js">{/layout:set}
{layout:set name="browser_title"}{{channel}_browser_title}{/layout:set}
{layout:set name="seo_description"}{{channel}_seo_description}{/layout:set}
{layout:set name="page_title"}{{channel}_page_title}{/layout:set}
{layout:set name="body_content"}{{channel}_body_text}{/layout:set}
{/exp:channel:entries}

更新: 您可以将包装器模板放在您喜欢的任何模板组中。我基本上有一个名为 _partials 的文件夹,其中包含我嵌入到其他模板中的所有模板。 假设您有这样的模板组设置:

_partials
    -_wrapper
    -_another_template
    -_some_other_template
blog
    -index
    -item
news
    -index
    -item

在博客或新闻的每个模板中,您可以使用 {layout="_partials/_wrapper"} 嵌入相同的包装器模板,因为它只需要 template_group/template_name 作为输入。

如果您需要更多帮助,请前往 https://expressionengine.stackexchange.com/ 获取更具体的 EE 建议