Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
[howto] Nice, Custom Page Titles
I’ve been struggling with these for a while, and always ended up struggling through this for each of the websites I built. Finally I’ve come up with code that will satisfy my most broad needs, and that I can trim if I need something simple.
The following code will produce nice page titles for the following cases:
- when on the home page,
- when on the section lander,
- when on the category lander within a section (for articles that are within categories, and sections),
- when the individual article page.
The code can be found in this GitHub gist.
I hope this will help someone.
PS
I’m linking to a gist because if someone suggests improvements or I change something I can just change the gist, instead of changing code in several places.
Offline
Re: [howto] Nice, Custom Page Titles
Hi Dragan, nice snippet and nice idea of using a gist.
In your txp:if_individual_article section, your context is very clear so you should be able to use the standard txp:article
tag instead of txp:article_custom id='<txp:article_id />'
. As a sidenote, there’s no need for limit="1"
with txp:article_custom if you are explicitly telling it show one article id with id="123"
.
In the back of my mind, I vaguely remember that txp:if_individual_article
might actually be enough context for the txp:title
and txp:category2
tags so you may also be able to do without txp:article too, but I couldn’t find that documented just now so pls test that first before changing your code.
TXP Builders – finely-crafted code, design and txp
Online
Re: [howto] Nice, Custom Page Titles
Hi Jakob, thanks for the reply!
I was getting strange results when I was using simply <article_custom section='<txp:section />'>
, as TXP always output the title of the last article that’s posted in the section currently viewed. The <txp:article_custom id='<txp:article_id />'>
makes sure I always get the proper article title.
I will update the gist with your proposed solution for removal of the limit="1"
attribute.
Offline
Re: [howto] Nice, Custom Page Titles
EDIT: you have a slight mixup in your corrections. You have txp:article with id (line 41) as the opening tag and txp:article_custom (line 59) as the closing tag. txp:article is context-sensitive, meaning that it knows which article it should show in if_individual_article view. Consequently it doesn’t need (or have) an id attribute.
What I meant was to use txp:article (without the _custom) so the if_individual_article part of the code would read:
<txp:if_individual_article>
<txp:hide>this is an individual article</txp:hide>
<txp:article>
<txp:title />
<txp:hide>check to see if the article is in a category</txp:hide>
<txp:if_article_category>
<txp:hide>it is, display category2 title (for you this may be category1)</txp:hide>
— <txp:category2 title="1" />
</txp:if_article_category>
<txp:hide>display article's section title</txp:hide>
— <txp:section title="1" />
</txp:article>
</txp:if_individual_article>
And you may find that it works entirely without txp:article or txp:article_custom:
<txp:if_individual_article>
<txp:title />
<txp:if_article_category>
— <txp:category2 title="1" />
</txp:if_article_category>
— <txp:section title="1" />
</txp:if_individual_article>
Last edited by jakob (2012-01-29 19:03:36)
TXP Builders – finely-crafted code, design and txp
Online
Re: [howto] Nice, Custom Page Titles
Thanks again Jakob, I didn’t know that if you are in an individual article context, you don’t need any of the txp:article
tags (if you need to display information related to the article currently being displayed of course).
I have updated the gist again.
Offline
Re: [howto] Nice, Custom Page Titles
I didn’t know that if you are in an individual article context, you don’t need any of the txp:article tags
I think that is the case If my memory serves me correctly, but my memory is, well …
I’d test it to be sure.
TXP Builders – finely-crafted code, design and txp
Online
Re: [howto] Nice, Custom Page Titles
It works like a charm, tested!
Offline
Re: [howto] Nice, Custom Page Titles
Another option – set the title, keywords, description when the output content.
aks_cron : Cron inside Textpattern | aks_article : extended article_custom tag
aks_cache : cache for TxP | aks_dragdrop : Drag&Drop categories (article, link, image, file)
Offline
Pages: 1