ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Inserting meta-tags into a universal html header template - newbie question

August 06, 2010 12:26pm

Subscribe [3]
  • #1 / Aug 06, 2010 12:26pm

    SpectraDisplays

    24 posts

    ok, so I have followed the tutorials through and generally have much of this figured out, I have a single html page header chunk for all pages, so I can make major site changes to the template easily.

    For each individual channel, I want custom fields for meta data, but how do I select to include these in the generic html header, when this is a separate included file on its own and just pulled into each individual channel template??

    I hope that make sense 😖

    Thanks for looking

  • #2 / Aug 09, 2010 2:35pm

    John St-Amand

    865 posts

    ok, so I have followed the tutorials through and generally have much of this figured out, I have a single html page header chunk for all pages, so I can make major site changes to the template easily.

    For each individual channel, I want custom fields for meta data, but how do I select to include these in the generic html header, when this is a separate included file on its own and just pulled into each individual channel template??

    I hope that make sense 😖

    Thanks for looking

    You can do this the same way as you pass, for example, the title of each post.  Do it with the embed.  So in the embed for the HTML Header, you also pass a declared variable.  Something like:

    {embed="embeds/html-header" the_entry_meta_title="{exp:channel:entries channel="{your_channel_name}" orderby="date" sort="desc" dynamic="off" disable="member_data|categories|trackbacks|category_fields"}{title}{/exp:channel:entries}"}

    That would pass the title of your post as a variable into your embedded HTML Header embed.  then in the HTML Header, you would then used this embedded variable in the template where you need it.  So you could use it in the HTML title tag, and you could also use it in the meta title tag.  Where ever you need the title to appear, you just use:

    {embed:the_entry_meta_title}

    And you can repeat this approach with other fields, such as a meta description or meta keywords list.  They just need to be part of your field group for the channel in question.

    In addition to this approach, i usually also setup a default list, which is stored separately - basically adding an “IF” statement that says if there is a value for this field, use it, and if there is not, use this default value instead.  That’s a bit more work to setup, but if you’re not confident that the meta data will be remembered each and every time an entry is added, at least then you have something going into the meta data tags and it’s not leaving them blank.

    That’s just how I have done it though.  Other EE’ers may have other suggestions.

    Hope that helps,
    John

  • #3 / Aug 09, 2010 3:15pm

    ender

    1644 posts

    I created the string plugin to handle this sort of thing elegantly.  It essentially lets you create variables in your main template that can be manipulated and used in your embedded header template.

    You should be able to follow the ExpressionEngine Page Titles & Meta Descriptions sample code on the code examples page to do use this plugin to do what you need to do.

    There are a number of other meta-tag-oriented addons out there, but I haven’t found one that is flexible enough to use on more complex sites. The string plugin doesn’t assume or enforce a particular structure so you’re free to use data from weblog/channel entries, categories, hard-coded values, global variables, snippets, etc.

    From the code examples page:

    main template:

    {embed="includes/head"}
    {exp:channel:entries channel="blog"}
        {exp:string:set name="title" default="{title}"}
            {blog_page_title}
        {/exp:string:set}
        {exp:string:append name="title" separator="|"}
            {weblog}
        {/exp:string:append}
        {exp:string:set name="meta_desc"}
            {blog_meta_desc}
        {/exp:string:set}
    {/exp:channel:entries}

    embedded head template:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang="en">
    <head>
        {exp:string:append name="title" separator="|" text="Company Name"}
        <title>{exp:string name="title"}</title>
        {exp:string:output name="meta_desc"}
            {if string != ""}
                <meta name="description" content="{string}" />
            {/if}
        {/exp:string:output}
    </head>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases