1

I want to be able to populate my meta tags with the information for the specific page that the user is visiting. I obviously can't just plug a variable in there without it being in some kind of a channel entries loop, but I don't want to have to specify the channel because that defeats the purpose of having it dynamically populated. Is there a way to grab the channel based on the page the user is on? I'd basically want to have something like this:

{exp:channel:entries channel="{channel}"}
<meta property="og:title" content="{title}" />
...etc.
{/exp:channel:entries}
9

Personally I'd use SEO Lite - then only this is needed in the header:

{exp:seo_lite use_last_segment="y"}

And then this in the config file / SEO Lite template:

EE2 example (with focuslab llc master config): https://gist.github.com/bjornbjorn/4039233

EE3 example can be found here: http://wedoaddons.com/seo-lite/extra-tags

http://devot-ee.com/add-ons/seolite

.. but then again I might be biased as I'm the dev behind the addon ;-)

7
  • If I could up-vote this answer more than once, I would.
    – Alex Kendrick
    Nov 19 '13 at 15:15
  • Can you give any clarity to the seo_lite config file you posted a link to? I am researching your solution a bit deeper and I don't understand how to implement the array you posted a link to. When I try and look at the markup, the extra fields show up as {extra:desc}. I'm not sure where the "extra" part of that tag is coming from? any clarification you can provide would be great.
    – hyphen81
    Nov 20 '13 at 3:06
  • I always use FocusLab's Master Config, which makes it easy to have multiple installations (local, staging, prod) and one config for each. However, if you don't that code would go in system/expressionengine/config/config.php and the array would be named $config not $env_config. That array is basically a PHP array of channel_id's (the database id of each channel) and then the field_id's (the database id of the custom field) for the various "extra tags" (ie. desc, image). That way SEO Lite knows where to pull the og:image, og:description from for each channel entry. Nov 20 '13 at 10:09
  • that works perfectly! awesome! I'm not sure what's supposed to happen here, but I think after looking at both solutions provided here, yours is a better fit for my situation, unfortunately I had already marked Derek Hogue's answer as correct. One last question. How can I override the {extra:desc} tag for a single template?
    – hyphen81
    Nov 20 '13 at 12:06
  • {extra:desc} can't be overridden as it is now, but you can use conditionals in the template as you usually do .. normally if you hook it up to a custom field though it will just be empty if that is left empty .. what's the use-case you have for this if you don't mind me asking? .. btw it is possible to change the accepted answer according to this: meta.stackexchange.com/questions/5234/… Nov 20 '13 at 21:13
3

I usually embed the "header" portion of each page, since most of that content is universal. So in this case, you can just use embed parameters to pass any meta tags dynamically.

{exp:channel:entries channel="foo"}
    {embed="_global/_header" title="{title}" description="{page_summary}" thumbnail="{page_thumbnail}"}
    <h1>{title}</h1>
    // etc
{/exp:channel:entries}

Then in your _header template:

// opening html tag, etc
<meta name="og:title" content="{embed:title}" />
<meta name="og:description" content="{embed:description}" />
<meta name="og:image" content="{embed:thumbnail}" />
<meta name="description" content="{embed:description}" />
<meta name="thumbnail" content="{embed: thumbnail}" />
<title>{if embed:title}{embed:title} - {/if}My Great Site</title>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.