Textpattern tips, tutorials and code snippets

Simple page titles

During the current update taking place on the TXP Tips site, I found that we needed to change quite a few things that had become stagnant. One of these items was the page title, which is easy to output using Textpattern’s page_title tag.

More granular control

However, if you want a little more control over the output, one option is to use the following snippet, which essentially checks which page we are on in Textpattern and then produces a custom page title.

Dragon Babic previously wrote his own version of custom page titles which is very good. This snippet performs a further check that we need here on TXP Tips – whether or not we are on a search or author page.

The code

<txp:if_search>
	<txp:variable name="page-title" value='Search | TXP Tips' />
<txp:else />	
<txp:if_category>
	<txp:variable name="page-title" value='<txp:category title="1" /> | TXP Tips' />
<txp:else />
<txp:if_author>
	<txp:variable name="page-title" value='<txp:author title="1" /> | TXP Tips' />
<txp:else />
<txp:if_article_list>
	<txp:if_section name=",">
		<txp:variable name="page-title" value='Home | TXP Tips' />
	<txp:else />
		<txp:variable name="page-title" value='<txp:section title="1" /> | TXP Tips' />
	</txp:if_section>
<txp:else />
	<txp:variable name="page-title" value='<txp:title no_widow="0" /> | TXP Tips' />
</txp:if_article_list>
</txp:if_author>
</txp:if_category>	
</txp:if_search>

<title><txp:variable name="page-title" /></title>

Place the code in your default page or form for use before the closing </head> tag.

Most of the code should be self explanatory, although it is worth noting that the check for the default page <txp:if_section name=","> (the home page) is done within the <txp:if_article_list> conditional otherwise we always end up with Home | TXP Tips in the page title.

Edit: Please note that there is a plugin solution – arc_meta – that can be used instead of the above code which is based on internal TXP tags.

6 Comments Comment feed

Perhaps this could be simplified a bit by using <txp:arc_meta_title/> (andy-carter.com/txp/arc_meta). It should significantly reduce the number of conditionals used here; although you'd still need some as it wouldn’t handle things like the <txp:if_author> case.

Hi Andy thanks for the comment. Maybe you'd be interested to post your code sample for others to see and possibly use?

Generally I personally prefer to use internal Textpattern tags for this rather than a plugin so this solution may not suit everyone...however if your plugin could simplify the admittedly verbose code above it would be nice to know how we could do this?

Happy to post an example. Just trying to follow your example code above to replicate it with <txp:arc_meta_title /> but not sure your conditionals are correct. You seem to have two else statements relating to <txp:if_article_list> …; this is surely wrong?

Oh dear that's an error :(
Thanks for spotting the extra else statement! Fixed in the code above.

No problem. Looks better now.

I think that the above should be able to be rewritten using the arc_meta plugin as:

<txp:if_author>
<title><txp:author title="1" /> | TXP Tips</title>
<txp:else />
<txp:arc_meta_title />
</txp:if_author>

The title formats for each of the other cases can then be setup in the arc_meta plugin preferences (although the defaults aren't too far off what you've got here. Basically reducing 22 lines of code down to just 5.

Thanks a lot Andy. The tip above has been edited to include a reference to your plugin. Thanks!