Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2010-07-06 08:34:59

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Best practises with custom meta information

I use the following code for the dynamic parts of my meta info, pretty much covers all you’d need…

First, install etz_striptags plugin, then create custom fields ‘meta_title’, ‘meta_desc’ and ‘meta_robots’.

Put this code in your page template (or a ‘misc’ type form linked from the page template):

<txp:if_section name="">
    <txp:if_status number="200">
        <txp:article_custom section="homepage" status="sticky" form="meta_dynamic" />
    </txp:if_status>
    <txp:else />
    <txp:if_individual_article>
        <txp:article form="meta_dynamic" />
        <txp:else />
        <txp:article status="sticky" form="meta_dynamic" />
    </txp:if_individual_article>
</txp:if_section>

What the code above does is pull in article info even if the page is a sticky or the homepage (for that you’ll need to set up a section called ‘homepage’ and a sticky article for it and make sure that section is set to display on homepage).

Then set up an ‘article’ type form with the following in it:

<title>
<txp:if_custom_field name="meta_title">
    <txp:custom_field name="meta_title" />
    <txp:else />
    <txp:title />
</txp:if_custom_field>
 | <txp:site_name /></title>
<txp:if_custom_field name="meta_desc"><meta name="description" content="<txp:custom_field name='meta_desc' />" />
<txp:else />
<meta name="description" content="<txp:etz_striptags><txp:excerpt /></txp:etz_striptags>" />
</txp:if_custom_field>
<txp:if_keywords>
    <meta name="keywords" content="<txp:keywords />" />
    <txp:else />
    <meta name="keywords" content="your,default,keywords,here" />
</txp:if_keywords>
<txp:if_custom_field name="meta_robots">
    <meta name="robots" content="<txp:custom_field name='meta_robots' />" />
    <txp:else />
    <meta name="robots" content="index,follow,noodp" />
</txp:if_custom_field>
<txp:if_section name="">
    <txp:else />
    <txp:if_individual_article>
    <txp:else />
    <link rel="canonical" href="http://www.yourdomain.com/<txp:section />" />
    </txp:if_individual_article>
</txp:if_section>

So that code checks whether you have put a custom page title in ‘meta_title’ custom field, and if not uses the page title instead.

Then it checks whether you have put a custom description in ‘meta_desc’ and if not uses the page excerpt, which has all textile formatting stripped out using etz_striptags (you probably could go further and have a default description if the user has not filled in the excerpt by using <txp:if_excerpt> though you should encourage users to always fill out a unique description for each page for SEO anyway).

Next it checks whether the keywords field has been filled in, and if not uses a default selection of keywords.

It then uses an SEO recommended robots meta unless you override it by entering a custom instruction in ‘robots_meta’ custom field (for example you could use ‘none’ in the meta_robots custom field for any pages you did not want search engines to index).

Finally, another SEO recommendation, it makes sure section landing pages are canonical – which means search engines index only http://www.yourdomain.com/yoursection and not http://www.yourdomain.com/yoursection/ (note the extra trailing slash) as sometimes they might index both those URLs separately and dilute your search ranking.

Hope that helps,
Phil

Offline

Board footer

Powered by FluxBB