Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[howto] Article dependent meta description in page header
I had a feature request and found the solution myself by research in the forum. Here is my solution:
Original feature request for a meta description plug-in
I am looking for a plug-in which only generates a meta-description tag. Somehow a meta-description generator with the following features is not available.
Features:
- Some pre-defined leading text
- if article – The standard TXP keywords for that article (optional: custom field)
- if article list – some pre-defined keywords
- if search result – some pre-defined keywords
- Some pre-defined trailing text
- An option to cut the meta-description tag to a maximum length
The workaround solution. Not covering all points from the feature request but already satisfying (myself).
Create form ‘meta_articletitle’. Content:
<meta name="description" content="Some pre-defined leading text: <txp:title /> - <txp:keywords /> - Some pre-defined trailing text" />
Create form ‘meta_description’. Content:
<txp:if_individual_article>
<txp:article form="meta_articletitle" />
</txp:if_individual_article>
<txp:if_article_list>
<meta name="description" content="Some pre-defined leading text: <txp:sitename /> - Some pre-defined trailing text" />
</txp:if_article_list>
Call form ‘meta_description’ in page header:
<txp:output_form form="meta_description" />
Of course you can play around with the used tags in the forms ‘meta_articletitle’ and in ‘meta_description’ as you like. In form ‘meta_description’ you must make sure to only use tags which are allowed in the header context!
Edit: Fast add-on tip for lists (beside all the if/else conditions you may want to add): Add page number <txp:page_url type="pg" />
to cover multiple page lists.
You can see the results on my German page sankt-georg.info.
Coments and refinements welcome!
Last edited by merz1 (2010-10-28 12:26:20)
Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML
Offline
#2 2006-06-01 21:44:20
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] Article dependent meta description in page header
I moved this to How-tos and Examples. There is btw a similar post about customizing page titles.
Offline
Re: [howto] Article dependent meta description in page header
Propaply a little plugin will do this job better.
I didn’t test it, but something like should work
<pre><code> function my_meta($atts,$thing){ global $pretext; if(emty($pretext[‘id’])){ //if empty, we handle a list return ‘<meta description=“standard text” /> ‘; } else{ //we have to fetch the custom-field, //because $thisarticle is not defined $customMeta = fetch(‘custom_x’,‘textpattern’,‘ID’,$pretext[‘id’};); return “<meta description=’$customMeta’ />”; } } </code></pre>Place <my_meta />
into the head of your page.
This should be a little faster, beauce there is only one db-query.
If you call <article />
there are some more and you don’t need to call conditional tags
Offline
#4 2006-06-02 17:17:30
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] Article dependent meta description in page header
I would like to try this, Bastian, but does it mean that I have to enter my “standard text” in the plugin code? And replace ‘x’ in custom_x with the number of the customfield I suppose? Or should that be the name of the custom field?
Offline
Offline
Re: [howto] Article dependent meta description in page header
If someone wants such a plugin, let me know.
I use a plugin like this to deliver JS and CSS by section or atricle. So i could change thatone.
Offline
#7 2006-06-02 18:50:25
- Ace of Dubs
- Member
- Registered: 2006-04-17
- Posts: 446
Re: [howto] Article dependent meta description in page header
Bastian wrote:
If someone wants such a plugin, let me know.
I use a plugin like this to deliver JS and CSS by section or atricle. So i could change thatone.
This sounds very interesting indeed. I am looking for a solution to dynamically switch css for different sections. Would such a plugin be difficult to write?
Offline
#8 2006-06-02 18:52:10
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] Article dependent meta description in page header
You don’t need a plugin for that?! What about setting it in Presentation > sections? Or do I completely misunderstand you?
Offline
Re: [howto] Article dependent meta description in page header
I am looking for a solution to dynamically switch css for different sections. Would such a plugin be difficult to write?
Switching by sections isn’t difficult, because it is build-in in txp :)
You can define a css for each section
Adding CSS or JS for each article is a little more difficult.
But not much.
I have a custom-field wich contains CSS or JS files, if a article needs them.
For example this one contains CSS and JS fiels wich are not included by the parent section.
A plugin reads only the custom-field (like mentioned above) and includes the needed files into the head.
Offline
#10 2006-06-02 19:05:48
- Ace of Dubs
- Member
- Registered: 2006-04-17
- Posts: 446
Re: [howto] Article dependent meta description in page header
Yah..I misworded my post. I would like to assign css to sections and certain articles as well.
I just discovered the joy of custom fields today…never occured to me to use them to assign css on the fly. Probably because I dont have this plugin of yours ;)
Offline
Re: [howto] Article dependent meta description in page header
This is the plugin i use at my site.
I renamed one custom-field to “CSS & JS” and stored there the files i need to display the articles proper.
The plugin regognize this pattern in custom-field:
CSS:a-css;JS:a-js
Place <txp:bas_css />
and/or <txp:bas_js />
into you head and it will output:
<script src="/javascript/per-article/a-js.js" type="text/javascript"></script>
and/or
<link rel="stylesheet" href="/stylesheets/per-article/a-css.css" type="text/css" />
Note: I wrote this one for my personal use. Every pathes are hardcoded and have to be adjusted to fit to your site.
This “ if($is_article_list == true){
“-part i had included, because sometimes i use only JS and script
must be in page’s head
If you use css within article you will recive invalid xhtml.
Offline
#12 2006-06-02 20:17:38
- Ace of Dubs
- Member
- Registered: 2006-04-17
- Posts: 446
Re: [howto] Article dependent meta description in page header
Thanks Bas!
About to head out for a beer. I will give this a try tomorrow afternoon.
Offline