【问题标题】:How to automatically show Title of the Entries/Articles in the Browser Title Bar in ExpressionEngine 2?如何在 ExpressionEngine 2 的浏览器标题栏中自动显示条目/文章的标题?
【发布时间】:2011-05-31 16:00:51
【问题描述】:

如何在 ExpressionEngine 中输出条目的标题并将其显示在浏览器的标题栏中?

这是我页面标题的内容:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test Site</title>
    <link rel="stylesheet" href="{stylesheet=site/site_css}" type="text/css" media="screen" />
</head>

我需要让每个页面在浏览器的标题栏中显示条目的标题——我该如何实现?


部分更新代码:

这是我的做法:

{exp:channel:entries channel="news_articles" status="open|Featured Top Story|Top Story" limit="1" disable="member_data|trackbacks|pagination"}

{embed="includes/document_header" page_title=" | {title}"}

<body class="home">
<div id="layoutWrapper">
    {embed="includes/masthead_navigation"}
    <div id="content">
        <div id="article">
            <img src="{article_image}" alt="News Article Image" />
            <h4>{title}</h4>
            <h5><span class="by">By</span> {article_author}</h5>
            <p>{entry_date format="%M %d, %Y"} -- Updated {gmt_edit_date format="%M %d, %Y"}</p>                    
            {article_body}    
{/exp:channel:entries}
        </div>

你怎么看?

【问题讨论】:

    标签: expressionengine


    【解决方案1】:

    另一种相对较新的解决方法是使用 Stash 插件和模板部分方法。这种方法将您打倒到一个嵌入,并具有为您提供集中式“包装器”模板的额外优势 - 基本上每个主要页面布局都有一个。下面的示例假设您已经简单地添加了自定义字段来处理您希望注入到标头中的任何条目特定的元数据。考虑到这个想法,下面是我最近一直在应用的基本结构的简化视图:

    在您的模板中应用 EE 标签来确定发送到内部包装器的逻辑

    {embed="embeds/.inside-wrapper"}
    
    {exp:channel:entries channel="channel_name" limit="1" dynamic="yes" disable="whatever|you|can|live|without"}
    
    {!-- ENTRY SEO META DATA --}
    {exp:stash:set name="entry_seo_title" scope="site"}{cf_channelprefix_seo_title}{/exp:stash:set}
    {exp:stash:set name="entry_seo_description" scope="site"}{cf_channelprefix_seo_description}{/exp:stash:set}
    {exp:stash:set name="entry_seo_keywords" scope="site"}{cf_channelprefix_seo_keywords}{/exp:stash:set}
    
    {!-- ENTRY/PAGE CONTENT --}
    {exp:stash:set name="entry_body_content" parse_tags="yes" parse_conditionals="yes" scope="site"}
    Your page content here
    {/exp:stash:set}
    
    {/exp:channel:entries}
    

    然后在您的包装模板中,该模板最终将包含您所有的包装 HTML,但可以分块为 sn-ps。对于像标题这样的东西,因为它将与其他包装模板共享,例如:

    <html>
    <head>
    <title>{exp:stash:get name="entry_seo_title"}</title>
    <meta name="description" content="{exp:stash:get name="entry_seo_description"}" />
    <meta name="keywords" content="{exp:stash:get name="entry_seo_keywords"}" />
    </head>
    
    <body>
    
    {exp:stash:get name="entry_body_content"}
    
    </body>
    </html>
    

    【讨论】:

    • 谢谢,我想试试 Stash。这会很有帮助。
    【解决方案2】:

    如果您只想显示您的 ExpressionEngine 站点的名称(在 CP 主页 > 管理 > 常规配置中定义),请使用 site name global variable

    &lt;title&gt;{site_name}&lt;/title&gt;

    如果您只想显示给定频道的当前条目标题,请使用以下命令:

    <title>
        {exp:channel:entries channel="channel_name" limit="1" dynamic="yes"}
            {title}
        {/exp:weblog:entries}
    </title>
    

    许多网络开发人员将使用Embed Variable with an Embedded Template 将`{entry_title} 传递给全局嵌入模板,从而允许dynamic page title

    {embed="includes/header" title="{exp:channel:entries channel="{channel_name}"}{title}{/exp:channel:entries}"}
    

    如果您使用的是 EE2,SEO Lite Module 只需一行代码即可为您处理所有繁重的工作:

    <html lang="en">
    <head>
        <meta charset="utf-8" />
        {exp:seo_lite url_title="{url_title}"}
    </head>
    

    其他解决方案包括Low Title Plugin(EE1, EE2)

    【讨论】:

    • 谢谢瑞恩。我正在采用嵌入变量的方式。与使用嵌入变量相比,使用 Low Title 或 SEO Lite 是否更好?
    • 每个嵌入都需要通过 ExpressionEngine 模板解析器进行查询和迭代,这可能导致overhead and performance issues。我更喜欢 SEO Lite 模块,因为我发现它非常“轻量级”,并且只使用一个数据库查询来获取其所有输出。作为额外的好处,您可以获得所有SEO features included with the module
    • 谢谢。我一定会研究 SEO lite 模块。你认为它足够还是我应该使用 NSM Better Meta ee-garage.com/nsm-better-meta
    • 如何使用 SEO lite 显示“站点名称 | 文章名称”和“站点名称 | 类别名称”?
    【解决方案3】:

    Ryan 的 embed 方法的一个补充(这绝对是最灵活的方法):您可以在查看单个条目时将大部分页面包装在 {exp:channel:entries} 标记中,从而避免额外(且昂贵)的 channel:entries称呼。所以它看起来更像这样:

    {exp:channel:entries channel="channel_name" limit="1"}
        {embed="includes/header" title="{title}"}
        <h1>{title}</h1>
        {page_content}
        {embed="includes/footer"}
        {if no_results}{redirect="404"}{/if}
    {/exp:channel:entries}
    

    【讨论】:

      【解决方案4】:

      NSM Better Meta 是将通道元数据传递给标签的一种更完整的方式。

      对于较小的网站,我使用 String 插件。

      https://devot-ee.com/add-ons/string

      非常简单的语法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多