Yikes, Expression Engine Dynamic Site Titles
I wish there was a tag to use in template in Expression Engine to create nice descriptive titles, sadly there is not. But you can still create good looking titles using exp tags. Here is how I created dynamic site titles using Expression Engine. Basically I wanted each page/request to have a unique site title. It’s good for search engines, bookmarking, and analytics.
This is the format I wanted it in:
Ecsyle | | |
The category should only appear if we are making a category request, and the title should only appear if we are viewing a single entry.
Here is the code, then we can step through it to see how it works:
<title>Ecsyle
{exp:weblog:info weblog="current_webblog" rdf="off"} | {blog_title}{/exp:weblog:info}
{exp:weblog:entries rdf="off" weblog="current_webblog" orderby="date" sort="desc" limit="1" disable="custom_fields|member_data|pagination|trackbacks"}
{if category_request}
{exp:weblog:category_heading weblog="current_webblog"} | {category_name}{/exp:weblog:category_heading}
{/if}
{/exp:weblog:entries}
{exp:weblog:entries rdf="off" weblog="current_webblog" orderby="date" sort="desc" limit="1" disable="custom_fields|member_data|pagination|trackbacks" require_entry="yes"}
| {title}
{/exp:weblog:entries}
</title>
The very first thing is “Ecsyle”. I simply wanted this on every single page. Hardcoded text works fine. First part is done. Easy. Next is the Section, or Blog name.
{exp:weblog:info weblog="current_webblog" rdf="off"} | {blog_title}{/exp:weblog:info}
The weblog information tag provides us with lots of information about the blogs we have created. We are only interested in the Title of the Blog so we just add | (with spaces) in between the weblog information tags to print out the name of our Blog. Part two of our title is done.
The next step is to find out if we are requesting a category. Our conditional variable has to be inside the weblog entries tag to be processed so we wrap the weblog entries tag around our condition.
{exp:weblog:entries rdf="off" weblog="current_webblog" orderby="date" sort="desc" limit="1" disable="custom_fields|member_data|pagination|trackbacks"}
{if category_request}
{exp:weblog:category_heading weblog="current_webblog"} | {category_name}{/exp:weblog:category_heading}
{/if}
{/exp:weblog:entries}
The last part of our title is the blog entry title. This is just another weblog entries tag and us looking for the weblog title. The big difference between this and the category title above is that we will require a single entry with require_entry="yes” in our weblog entries tag so that the title doesn’t display on any other page but the single entry page.
{exp:weblog:entries rdf="off" weblog="current_webblog" orderby="date" sort="desc" limit="1" disable="custom_fields|member_data|pagination|trackbacks" require_entry="yes"}
| {title}
{/exp:weblog:entries}
So that’s it. Pretty easy. I am not a fan of the additional MySQL overhead just to generate a clean title, but it is what it is. Take a look around this site to see the title in action.









Comments