如何在 ExpressionEngine 2 的浏览器标题栏中自动显示条目/文章的标题?

发布于 2024-11-10 13:16:33 字数 1253 浏览 5 评论 0原文

如何在 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>

你觉得怎么样?

How would I output the title of an entry in ExpressionEngine and display it in the browser's title bar?

Here is the content of my page's header:

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

What I need is for each page to display the title of the entry in my browser's title bar — how can I achieve that?


Part of UPDATED Code:

Here is how i have done it :

{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>

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦欢 2024-11-17 13:16:33

另一种相对较新的解决方法是使用 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,但可以分为片段。对于诸如标头之类的内容,因为它将与其他包装模板共享,例如:

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

Another relatively new way to tackle it is using the Stash add-on and a template partials approach. This method knocks you down to one embed, and has the added advantage of giving you a centralized "wrapper" template - one for each major page layout, basically. The example below assumes you've simply added custom fields to handle any entry-specific meta data you're looking to inject into the header. With this idea in mind, here's a simplified view of the basic structure I've been applying recently:

In your template you apply EE tags to determine the logic of what gets sent to the inside-wrapper

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

And then in your wrapper template, which would ultimately contain all your wrapping HTML but could be chunked into snippets. for something like the header since it would be shared with other wrapper templates, for example:

<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>
巾帼英雄 2024-11-17 13:16:33

如果您只想显示 ExpressionEngine 站点的名称(如 CP Home > Admin > General Configuration 中定义),请使用 站点名称全局变量:

{site_name}

如果要显示仅来自给定频道的当前条目标题使用以下内容:

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

许多 Web 开发人员将使用 嵌入变量嵌入模板将“{entry_title}”传递到全局嵌入模板,从而允许动态页面标题

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

如果您使用 EE2,则 SEO Lite 模块 只需一行代码即可为您处理所有艰苦的工作:

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

其他解决方案包括 低标题插件 (EE1,EE2)

If you want to show just the name of your ExpressionEngine site (as defined in CP Home > Admin > General Configuration) use the site name global variable:

<title>{site_name}</title>

If you want to display just the current entry title from a given channel use the following:

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

Many Web Developers will use an Embed Variable with an Embedded Template to pass the `{entry_title} to a global embed template, allowing for a dynamic page title:

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

If you're using EE2, the SEO Lite Module takes care of all the hard work for you with a single line of code:

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

Other solutions include the Low Title Plugin (EE1, EE2).

半世蒼涼 2024-11-17 13:16:33

Ryan 嵌入方法的一项补充(这绝对是最灵活的方法):您可以在查看单个条目时将页面的大部分内容包装在 {exp: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}

One addition to Ryan's embed method (which is definitely the most flexile method): chances are you can wrap most of your page in an {exp:channel:entries} tag when viewing an individual entry, avoiding the additional (and expensive) channel:entries call. So it would look more like this:

{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}
迷乱花海 2024-11-17 13:16:33

NSM Better Meta 是将通道元数据传递到标签的更完整的方法。

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

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

语法非常简单。

NSM Better Meta is a more complete way to pass channel meta data to the tag.

For smaller sites, I use the String plugin.

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

Very simple syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文