Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
How to set meta elements
Hi friends,
so I was trying to add <title>
, description
and keywords
elements to the <head>
in my pages, but I found it really hard to set up.
I would like to present you my solution and ask you if I had just complicated the whole thing and if there are some better solutions you could suggest me.
So far this solution seems working great, just wonder if it is the right solution.
Before the code snippet, a little background on how the web site is set.
I choose the classic solution of a section based web site, with landing page for section made with sticky articles and sub pages made with section’s articles.
The home page is just default showing article_custom
portions forming the whole page.
Blog, as above, is just a section with landing page and single articles, no particular archive page (just categories, so default blog metas are fine there too).
Search results are shown in default section, so I don’t care about the home title there as well.
I used glz_custom_fields (invaluable plugin) to have as many custom fields I needed (such as page_title
one or a description
textarea).
set_meta
is there to allow to choose which articles bring metas with them since I also used several articles to build sidebars specific to each page or footer buckets.
That said, the code:
<head>
…
<txp:if_section name="">
<title><txp:output_form form="page_title_home"/> | <txp:site_name /> - <txp:site_slogan /></title>
<meta name="description" content="<txp:output_form form="bs_home_description" />" />
<meta name="keywords" content="<txp:output_form form="bs_home_keywords" />" />
<txp:else />
<txp:if_individual_article>
<txp:article_custom section='<txp:section />' id='<txp:article_id />' set_meta="true">
<txp:if_section name="blog">
<title>My Blog: <txp:title /> | <txp:site_name /> - <txp:site_slogan /></title>
<txp:else />
<title><txp:custom_field name="page_title" /> | <txp:site_name /> - <txp:site_slogan /></title>
</txp:if_section>
<meta name="description" content='<txp:custom_field name="description"/>' />
<meta name="keywords" content="<txp:keywords />" />
</txp:article_custom>
<txp:else />
<txp:article_custom section='<txp:section />' status="sticky" set_meta="true">
<title><txp:custom_field name="page_title" /> | <txp:site_name /> - <txp:site_slogan /></title>
<meta name="description" content='<txp:custom_field name="description"/>' />
<meta name="keywords" content="<txp:keywords />" />
</txp:article_custom>
</txp:if_individual_article>
</txp:if_section>
As always, thank you all for your help, suggestions and critics.
Giuseppe
Offline
Re: How to set meta elements
Have you taken a look at rah_metas. I use a combination of rah_metas and custom fields. rah_metas makes it a little easier to tidy up some of the logic so I can use a custom field (meta_description) if it exists, and fall back to the except or body if the excerpt doesn’t exist.
The page title is a little more difficult, especially for section landing pages. What I do here is have another custome field “landing_page” and I set it to “section” or “home” and then pull the meta info from that article.
I just need to get rel=canonical into the section landing pages to avoid duplicate content.
I don’t bother with the keywords meta tag. It has no SEO value any more.
Last edited by douglgm (2011-09-11 07:08:21)
Offline
Re: How to set meta elements
Hi Doug, thanks a lot for pointing me to rah_metas, I think I’ll use to quick some stuff like automatic description and keywords filling, but I still have to use the above mentioned logic to select from which articles to pull infos.
Thanks again, it will dramatically improve my code.
Btw, thanks a lot to Jukka as well for making such useful plugins.
So, any other suggestion?
Offline
Re: How to set meta elements
This is the code I’m currently using on a site that I’m building. meta_description is a custom field as well as landing_page.
Landing page contains either “Home” to identify the article that is used to deliver static content to the home page or “Section” as a section’s landing page static content.
<!-- Page Metas -->
<txp:if_individual_article>
<txp:rah_metas description_from="meta_description,excerpt,body" />
<txp:else />
<txp:if_section name="default">
<txp:article_custom status="sticky" landing_page="Home">
<txp:rah_metas description_from="meta_description,excerpt,body" />
</txp:article_custom>
<txp:else />
<txp:article_custom status="sticky" landing_page="Section" section='<txp:section/>'>
<txp:rah_metas description_from="meta_description,excerpt,body" />
</txp:article_custom>
</txp:if_section>
</txp:if_individual_article>
I’m not doing any thing to manage canonical section landing pages yet, but would add this to the <txp:if_individual_article> section at the top of the above.
Setting the page title so that it’s SEO friendly is something else that’s more complicated than it should be! I wanted to have the option to set the <title> to be different from the article title so I’ve used another custom field called “meta_title”.
Didn’t want the site name in the <title> as I want to craft as appealing a SERPS snippet as I can!
<title>
<txp:if_individual_article>
<txp:custom_field name="Meta_Title" default='<txp:title no_widow="0"/>' />
<txp:else />
<txp:if_section name="default">
<txp:article_custom status="sticky" landing_page="home" form="landing_page" limit="1">
<txp:custom_field name="Meta_Title" default='<txp:title no_widow="0"/>'/>
</txp:article_custom>
<txp:else />
<txp:article_custom status="sticky" landing_page="section" form="landing_page" section='<txp:section/>' limit="1">
<txp:custom_field name="Meta_Title" default='<txp:title no_widow="0"/>'/>
</txp:article_custom>
</txp:if_section>
</txp:if_individual_article>
</title>
Hope this help. Let me know if you can spot any problems or improvements!
Offline
Re: How to set meta elements
caruso_g wrote:
Btw, thanks a lot to Jukka as well for making such useful plugins.
Thanks :-) (we are always watching — maybe)
douglgm wrote:
This is the code I’m currently using on a site that I’m building.
Very cool usage example.
Last edited by Gocom (2011-09-11 21:51:53)
Offline
Re: How to set meta elements
Hi guys, for future reference, following is the final version.
Even if Jukka version is really interesting, I needed a more granular control over what and from what do display. E.g. pages need a more specific title and description then a single post page since they will bring money to my client. Using Jukka plugin I would have been able to select which field to display.
@Jukka
About that, it would be interesting to select a name=meta_name
to be able to singularly render just one specific meta instead of all at once. Of course, I see the problem, one should be able to have something like filter_name=meta_name
to avoid to render twice a single meta.
douglgm wrote:
Didn’t want the site name in the <title> as I want to craft as appealing a SERPS snippet as I can!
@douglgm Why no site name in title for SERPS?
In any case, thank you all for your suggestions,
Gisueppe
<txp:if_section name="">
<title><txp:output_form form="page_title_home"/> | <txp:site_name /> - <txp:site_slogan /></title>
<meta name="description" content="<txp:output_form form="bs_home_description" />" />
<txp:else />
<txp:if_individual_article>
<txp:article_custom section='<txp:section />' id='<txp:article_id />' set_meta="true">
<txp:if_section name="blog">
<title>My Blog: <txp:title /> | <txp:site_name /> - <txp:site_slogan /></title>
<meta name="description" content='<txp:rss_auto_excerpt overrideexcerpt="1" stripbreakstabs="1" striptags="1" words="25" showlinkwithexcerpt="0" excerptwraptag="0" ending=" …"/>' />
<txp:else />
<title><txp:custom_field name="page_title" /> | <txp:site_name /> - <txp:site_slogan /></title>
<meta name="description" content='<txp:rss_auto_excerpt overrideexcerpt="1" stripbreakstabs="1" striptags="1" words="25" showlinkwithexcerpt="0" excerptwraptag="0" ending=" …"/>' />
</txp:if_section>
</txp:article_custom>
<txp:else />
<txp:article_custom section='<txp:section />' status="sticky" set_meta="true">
<title><txp:custom_field name="page_title" /> | <txp:site_name /> - <txp:site_slogan /></title>
<txp:if_custom_field name="description">
<meta name="description" content='<txp:custom_field name="description"/>' />
<txp:else />
<meta name="description" content='<txp:rss_auto_excerpt overrideexcerpt="1" stripbreakstabs="1" striptags="1" words="25" showlinkwithexcerpt="0" excerptwraptag="0" ending=" …"/>' />
</txp:if_custom_field>
</txp:article_custom>
</txp:if_individual_article>
</txp:if_section>
Offline
Re: How to set meta elements
I’m not sure I understand. Using my approach I have absolute control over the meta data displayed for both a section landing page and an article page. I also have a fall back when I haven’t explicitly set a meta title or meta description.
The problem with the site default Textpattern title is that it puts the sitename first. Ideally you’d want your keywords appearing as close to the beginning of the title as possible, and you’d want your title to answer the searchers query as directly as possible.
Unless your site is a known and trusted brand, you’re not really adding much having the site name in the title. My personal preference is to use the characters to make an appealing title as possible. After all, you don’t just want the SERPS rankings, you want the click-through too.
Offline
Re: How to set meta elements
caruso_g wrote:
About that, it would be interesting to select a
name=meta_name
to be able to singularly render just one specific meta instead of all at once. Of course, I see the problem, one should be able to have something likefilter_name=meta_name
to avoid to render twice a single meta.
You can already do that. When an attribute is set empty, the specific meta isn’t used. I.e. keywords_from=""
, description_from=""
.
Even if version is really interesting, I needed a more granular control over what and from what do display.
I’m not sure I understand either. Doug’s code does exactly same yours, and as Doug said, the method allows full control over the metadata.
Offline
Re: How to set meta elements
douglgm wrote:
I’m not sure I understand. Using my approach I have absolute control over the meta data displayed for both a section landing page and an article page. I also have a fall back when I haven’t explicitly set a meta title or meta description.
Yep, and I did the same. The difference is in our site structures. I have set no sticky page for default. It is composed, due to its complexity, by several article_custom
snippets.
I also used several articles to compose pages’ sidebars, footer buckets, a tabs navigation structures in page etc. I needed a way to control which article/page could render metas.
The problem with the site default Textpattern title is that it puts the sitename first. Ideally you’d want your keywords appearing as close to the beginning of the title as possible, and you’d want your title to answer the searchers query as directly as possible.
I see. That’s why I preferred to compose it myself.
Unless your site is a known and trusted brand, you’re not really adding much having the site name in the title. My personal preference is to use the characters to make an appealing title as possible. After all, you don’t just want the SERPS rankings, you want the click-through too.
Yep. I think I did the same here. I hope it’ll work as expected.
—
Gocom wrote:
You can already do that. When an attribute is set empty, the specific meta isn’t used. I.e.
keywords_from=""
,description_from=""
.
Ah, great. But will it still render other metas such author, rel, etc.?
I’m not sure I understand either. Doug’s code does exactly same yours, and as Doug said, the method allows full control over the metadata.
At least I’m not seeing something obvious, I think my code handle better my specific site structure. As said above, I don’t want and don’t need metas from articles used for constructing the site structure. I also set fallbacks for metas, trying to automate as much as possible my clients work.
Thanks again for your help.
Giuseppe
Offline
Re: How to set meta elements
caruso_g wrote:
Ah, great. But will it still render other metas such author, rel, etc.?
As I said, when a an attribute is set empty, that specific meta isn’t used. No other meta is even active by default other than keywords and description. Outputting metas individually makes no sense tho, even that it’s possible.
I think my code handle better my specific site structure
Your code does exactly the same.
Offline
Pages: 1