Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-03-08 23:30:45

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,640
Website GitHub

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:

  1. Iterate over the articles in one of the translated sections. (article_custom section=“langcode”).
  2. check the article_url_title. If it contains sec__urltitle then split it at __ and obtain the parts either side.
  3. output the link to the article as normal
  4. construct the original URL and add it to the sitemap entry as an alternate using /sec/urltitle as the URL.
  5. 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…
  6. … 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

#2 2025-03-09 06:24:42

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,142
Website GitHub Mastodon Twitter

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

#3 2025-03-09 06:30:14

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,826
Website GitHub

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

#4 2025-03-09 10:01:43

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,640
Website GitHub

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

Board footer

Powered by FluxBB