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:
- Some pre-defined leading text
- if single Textpattern article - The standard TXP keywords for that article (optional: custom field)
- if Textpattern article list - some pre-defined keywords
- if Textpattern search result - some pre-defined keywords
- Some pre-defined trailing text
- 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:sitename /> - Some pre-defined trailing text">
</txp:if_article_list></txp:if_individual_article>The Textpattern tag txp:sitename is allowed in the page header.
Textpattern / code for the page haeder
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!
Edited 24.3.07 because of comment.
Keywords / Categories / Technorati Tags:
Markus Merz, 2006, Textpattern, TXP, meta, description
powered by performancing firefox













Good Tip, Incorrect Template Tags
This is a great tip, but the way you use the template tags is incorrect.
<txp:article />and<txp:output_form />are both self-closing tags. The corrected code for the 'meta_description' form would be as follows:<txp:if_individual_article><txp:article form="meta_articletitle" />
<txp:if_article_list>
<meta name="description" content="Some pre-defined leading
text: <txp:sitename /> - Some pre-defined trailing text">
</txp:if_article_list>
</txp:if_individual_article>
You would call the form like this:
<txp:output_form form="meta_description" />I've used a slight variation on this technique for my own site, creating the metadata with a form of the 'article' type and then calling it with
<txp:article />instead of<txp:output_form />. Works like a charm, and it allowed me to get rid of the plugin I was using to create custom page titles. Thanks!Thanks for pointing this out!
Recently I have added a title for search pages. The tag for the standard search parameter is
<txp:q />.My form 'meta_description' now looks like:
<txp:if_individual_article>
<txp:article form="meta_artikeltitel" />
</txp:if_individual_article>
<txp:if_article_list>
<txp:if_search>
<meta name="description" content="Search: <txp:q /> - Site title" />
<txp:else />
<meta name="description" content="<txp:sitename /> - trailing information" />
</txp:if_search>
</txp:if_article_list>
For all Textpattern lovers ...
The result can be seen on my page (see above).
Update: Btw, if you want to use short excerpts for single articles in form="meta_artikeltitel" then you should have a look at the awesome TXP plugin rss_auto_excerpt. I think I will go and tweak my single article descriptions now ...
Where is the code tag gone?
Man, I can't believe it. This article was filled with code examples inside code tags. Now it is mostly useless junk which is left over from some p.com party. What a mess! My excuses to everybody interested.
Edit: The code content is still in the article source. But why does it NOT show up?
Edit II: OK, it's November 2007 and I can see the code again. Thanks!
txp:q is gone in Textpattern 4.0.6
<txp:q />is deprecated, now it is<txp:page_url type="q" />.<txp:page_url type="c" />now.My form 'meta_description' now looks like:
<txp:if_individual_article>
<txp:article form="meta_artikeltitel" />
<txp:article form="meta_artikeltitel" status="sticky" />
</txp:if_individual_article>
<txp:if_article_list>
<txp:if_search>
<meta name="description" content="Suche: <txp:page_url type="q" /> © Hamburg St. Georg Informationen" />
<txp:else />
<txp:if_section name="tag">
<txp:output_form form="meta_tagtitel" />
<txp:else />
<txp:if_category>
<meta name="description" content="Kategorie: <txp:page_url type="c" /> © Hamburg St. Georg Informationen" />
<txp:else />
<meta name="description" content="<txp:site_name /> - Hamburg St. Georg Nachrichten, Fotos, Reportagen, Interviews, Informationen" />
</txp:if_category>
</txp:if_section>
</txp:if_search>
</txp:if_article_list>
The line
<txp:article form="meta_artikeltitel" status="sticky" />is necessary. Otherwise 'sticky' articles will have no meta-description.Post new comment