【问题标题】:Expression Engine CMS How to populate meta tags dynamically?Expression Engine CMS 如何动态填充元标记?
【发布时间】:2018-06-11 21:05:24
【问题描述】:

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

我的结构是这样的: 我有一个在每个模板中调用的 {top} sn-​​p

在 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}

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

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

表达式引擎版本:2.11.2

【问题讨论】:

    标签: php seo expressionengine meta


    【解决方案1】:

    使用布局可以更轻松地做到这一点: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"} 嵌入相同的包装模板,因为它只需要模板组/模板名称作为输入。

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

    【讨论】:

    • 感谢您的宝贵时间。我很难弄清楚模板布局应该去哪里,结构。我应该在同一个模板组中创建一个具有名称布局的新模板吗? {layout="sub/blog"} {exp:channel:entries channel="pages" disable="categories|category_fields|custom_fields|member_data|pagination"} {layout:set name="seo_description"}{blog_seo_description}{/layout :set} {/exp:channel:entries} 然后从子/博客调用它? 再次感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多