-->

How to automatically show Title of the Entries/Art

2019-02-11 02:50发布

问题:

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?

回答1:

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



回答2:

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>


回答3:

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}


回答4:

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.