Textpattern tips, tutorials and code snippets

Page dependent header meta description

Editor’s note: The article below was first published on performancing.com. Please note the updates to the article in the comments on perfomancing.com.

Textpattern (TXP) – How to get a page dependent meta description

Maybe the logic can also be used for other Blog/CMS-systems …!?

The sense of a page dependent meta description is a better acceptance by search engines and the presentation of  your site is more nice because many search engines show the meta description tag as a site description in a result page.

I had a Textpattern feature request for a page dependent meta description plug-in but found the TXP solution myself through some research in the Textpattern forum by combining some hints. Here is my solution:

Original feature request for a meta description plug-in

I am looking for a Textpattern plug-in which only generates a meta-description tag for the header section of a web page. Somehow a Textpattern meta-description generator with the following features is not available (or I did not find it).

Textpattern (TXP) / meta description plug-in feature request:

  1. Some pre-defined leading text
  2. If single Textpattern article – The standard TXP keywords for that article (optional: custom field)
  3. If Textpattern article list – some pre-defined keywords
  4. If Textpattern search result – some pre-defined keywords
  5. Some pre-defined trailing text
  6. An option to cut the meta-description tag to a maximum length

The Textpattern workaround solution.

Not covering all points from the feature request but already satisfying (myself) …

The Textpattern problem is that it is not possible to use article related Textpattern tags directly for the page header. So I have to find a way around that Textpattern restriction …

Note: In Textpattern slang a form is like a ‘brick’ or like an ‘object’ which can be reused. You build your page templates in a very elegant way by combining forms. You don’t have to but this way the Textpattern template design is very clean and kind of object oriented – i.e. you only have to have one Textpattern form for a well designed AdSense ad or a side-note box and you only call this form and get the same result in many totally different page templates like main, archive, imprint or contact form.

The trick described below can be used for every Textpattern tag which you want to use in the page header context but which normally does not work. The trick is to render the content in a separate Textpattern ‘brick’ before you call it from a second Textpattern ‘brick’ which is rendered directly in the page header section (where only specific tags are allowed).

Textpattern / the preparation of the content

Create a Textpattern form ‘meta_articletitle’. Content:

<meta name="description" content="Some pre-defined leading text: <txp:title /> - <txp:keywords /> - Some pre-defined trailing text">

The Textpattern tags <txp:title> and <txp:keywords> are not allowed in the page header. If you use them you only get an empty output.

Textpattern / page dependant header output

This is the code which separates the output depending if it is a single article or an article list. There are many more possibilities to have if/else cases but this is the basic example.

Create a Textpattern form ‘meta_description’. Content:

<txp:if_individual_article>
<txp:article form="meta_articletitle" />
<txp:if_article_list>
<meta name="description" content="Some pre-defined leading text: <txp:site_name /> - Some pre-defined trailing text">
</txp:if_article_list></txp:if_individual_article>

The Textpattern tag txp:site_name is allowed in the page header.

Textpattern / code for the page header

Call Textpattern form ‘meta_description’ in the Textpattern page header (form): <txp:output_form form="meta_description" />

Notes:

Of course you can play around with the used Textpattern tags in the forms ‘meta_articletitle’ and in ‘meta_description’ as you like. In the form ‘meta_description’ you must make sure to only use Textpattern tags which are allowed in the (Textpattern) header context!

Example Textpattern site

You can see the results on my German Textpattern page sankt-georg.info The next step will be to get nicer page dependent titles for the single article pages in a very similar way.

Comments and refinements as always welcome!

2 Comments Comment feed

  • Stuart
  • 19 October 2008

The TXP tags “title” and “keywords” can be used in the “head” section of the page provided that you are in “individual_article” view so the use of the “if_individual_article” tag comes in handy.

  • Jan
  • 22 October 2008

I’m using a custom field to give each page its own meta description.

Add a comment

Use Textile help to style your comments