How to automatically show Title of the Entries/Articles in the Browser Title Bar in ExpressionEngine 2?

前端 未结 4 861
小蘑菇
小蘑菇 2021-01-03 09:12

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:

&l         


        
相关标签:
4条回答
  • 2021-01-03 10:00

    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.

    0 讨论(0)
  • 2021-01-03 10:04

    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}
    
    0 讨论(0)
  • 2021-01-03 10:05

    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>
    
    0 讨论(0)
  • 2021-01-03 10:11

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

    0 讨论(0)
提交回复
热议问题