Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Sitemap with hreflang
Slightly complex problem. In a multi-lingual site, rather than adding meta hreflang tags to every article that may or may not have a translation (thus bloating the page with tags as the number of translations grows) I’m thinking of adding the hreflang structures to my sitemap.
The sitemap itself is a fairly standard affair:
<txp:header name="Content-Type" value="application/xml; charset=utf-8" />
<?xml version="1.0" encoding="UTF-8"?>
<txp:variable name="site_sec">list, of, required, sections, here</txp:variable>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc><txp:site_url /></loc></url>
<txp:section_list sections='<txp:variable name="site_sec" />' break="">
<url><loc><txp:site_url /><txp:section />/</loc></url>
</txp:section_list>
<txp:article_custom section='<txp:variable name="site_sec" />' break="url" wraptag="" limit="99999">
<loc><txp:permlink /></loc>
<lastmod><txp:modified format="%Y-%m-%dT%H:%M:%SZ" /></lastmod>
</txp:article_custom>
</urlset>
Easy peasy.
Now, for each section that I know contains translations, I need to construct bi-directional hreflang entries: one for the current language and an alternate for the translation(s). Luckily, I have used a naming convention in constructing the translated articles so I know which section and article url title the original article is (so I can create backlinks). That convention is:
- Original: /sec1/article1
- Translation: /zh/sec1__article1
- Translation: /de/sec1__article1
- …
So to create the necessary hreflang entries I need to:
- Iterate over the articles in one of the translated sections. (article_custom section=“langcode”).
- check the article_url_title. If it contains
sec__urltitle
then split it at__
and obtain the parts either side. - output the link to the article as normal
- construct the original URL and add it to the sitemap entry as an
alternate
using /sec/urltitle as the URL. - stash that somehow (maybe?) so when the next part of the sitemap is created (
<txp:article_custom section="all, the, other, sections">
) if the article matches then we know it had a translation. In which case… - … construct the translated URL and add it to the sitemap entry as an
alternate
link.
I can resort to PHP but I wondered if there was any cunning tag-fu that can split (trim? breakby?) and capture the section and url_title portions either side of the __
so I can use them to make URLs.
This will get more unwieldy as the number of languages grows, but for now there’s only two: english and one other.
Alternatively, if anybody has any better ideas on how to achieve this sitemap, by all means show me the light. Everything I’ve tried so far has failed.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Sitemap with hreflang
what about the evaluate tag? <txp:evaluate query='contains("<txp:url_title type="req" />" != "__"'>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Sitemap with hreflang
I think you nearly had it: use breakby="__"
in combination with limit="1"
and for the second part offset="1"
:
<!-- section -->
<txp:variable name="section" breakby="__" limit="1"><txp:article_url_title /></txp:variable>
<!-- -article url -->
<txp:variable name="article_url" breakby="__" limit="1" offset="1"><txp:article_url_title /></txp:variable>
For the stashing, maybe you could add the values you want to the end of a variable, and then test later with if_variable
and match="any"
?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Sitemap with hreflang
Offset! That was it. Thank you so much.
I was paddling around with containers and breakforms and evaluate trying to use <txp:yield />
etc to get the first and then second items and failing big time. Didn’t think to split it up and use offset
. That’s brill. Works a charm.
I’m going to forego the stashing and take the hit on the duplicate URLs by just adding the bi-directional backref URLs immediately after the ones in the alternative language. Not sure if that invalidates the sitemap but the validator didn’t complain so I’ll suck it and see. If it can’t crawl properly, I may have to revisit it.
Thank you again.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Pages: 1