All add-ons currently require PHP 7.4 or greater.

On July 4th 2024 PHP 8.2 will be the new minimum requirement for all add-ons. Expect any add-on released after that date to require 8.2 or greater. Some releases may not immediately take advantage of 8.x specific features in PHP, which means you might, be able to continue using new releases in PHP 7.4, however, if you experience an error the first thing you should do is update to PHP 8.2 then create a support ticket if the error persists.

Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Generating XML sitemap - how to get a page link with language segment…?

Status Resolved
Add-on / Version Publisher 3.10.1 build 9ee00f49
Severity
EE Version 6.4.12

Gavin @ JCOGS

Jun 26, 2023

According to the documentation the variable {page_url} should include a language segment if that is defined for the site.  See here: https://docs.boldminded.com/publisher/docs/template-tags

This template code fragment generates XML sitemap entries for the languages defined for the site.

{exp:publisher:languages}
{exp:channel:entries orderby="edit_date" sort="desc" channel="homepage|pages"  publisher_lang_id="{id}" publisher_status="open" publisher_cache="yes"}
<url>
  <loc>{page_url}</loc>
  <lastmod>{edit_date format='%Y-%m-%d'}</lastmod>
  <xhtml:link rel="alternate" hreflang="{short_name}" href="{page_url}" />
</url>
{/exp:channel:entries}
{/exp:publisher:languages}

The code works in that it lists all the pages in the channels nominated for each language in turn, except that none of the URLs have a language segment. The default language is Spanish, so the English links should include a /en/ language slug. They do all work OK on live site. Here is an example of an English link from this listing:

<url>
  <loc>https://newfd.jcogs.net/hosting-prestashop</loc>
  <lastmod>2022-09-27</lastmod>
  <xhtml:link rel="alternate" hreflang="en" href="https://newfd.jcogs.net/hosting-prestashop" />
</url>

You can view the entire output at

Grateful if you could advise what I am doing wrong ...

 

#1

BoldMinded (Brian)

Are you using Structure or Pages module?

#2

Gavin @ JCOGS

Yes, Structure (5.3.0)

#3

BoldMinded (Brian)

I think the issue is that it gets page_url from the site_pages array, and it only loads a single language into that array at a time (this is core EE functionality that Publisher modifies), so even though you’re in the languages loop the site_pages array still only contains values for the current session language, not whatever the loop is. I think you’ll need to use the exp:publisher:translate_url tag in the loop. https://docs.boldminded.com/publisher/docs/template-tags#exp-publisher-translate_url

#4

Gavin @ JCOGS

Maybe I misunderstood your suggestion, but at least as implemented it doesn’t work. Here is what the updated template code looks like

<?xml version="1.0" encoding="UTF-8"?>
<urlset  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        >
    {exp:publisher:languages}
    {exp:channel:entries orderby="edit_date" sort="desc" channel="homepage|pages" publisher_lang_id="{id}" publisher_status="open" publisher_cache="yes"}
    <url>
        <loc>{exp:publisher:translate_url url='{relative_url}'}</loc>
        <lastmod>{edit_date format='%Y-%m-%d'}</lastmod>
        <xhtml:link rel="alternate" hreflang="{short_name}" href="{exp:publisher:translate_url url='{relative_url}'}" >
    </url>
    {/exp:channel:entries}
    {/exp:publisher:languages}
</urlset>

And here is an example of what it produces - note the language slug still missing …

<url> 
    <loc>https://newfd.jcogs.net/Informacion/contrato-de-vps</loc> 
    <lastmod>2022-09-27</lastmod> 
    <xhtml:link rel="alternate" hreflang="pt-pt" href="https://newfd.jcogs.net/Informacion/contrato-de-vps"> </xhtml:link>
</url>

Any other thoughts on how I can get this to work? All I’m after is a way to list out all the URLs for all entries in a channel for each language set for the site.

#5

BoldMinded (Brian)

Try {exp:publisher:translate_url url=’{relative_url}’ to_lang_id=”{id}”}

There is also a from_lang_id parameter. E.g. from_lang_id=”{publisher:current_language_id}”

#6

Gavin @ JCOGS

I am not trying to translate the URL titles - I simply want them to match what you need to enter to get the page to load. So ‘good’ output would be something like:

<url> 
    <loc>https://newfd.jcogs.net/vps-wordpress</loc> 
    <lastmod>2022-09-27</lastmod> 
    <xhtml:link rel="alternate" hreflang="es" href="https://newfd.jcogs.net/vps-wordpress"> </xhtml:link>
</url>
<url> 
    <loc>https://newfd.jcogs.net/en/vps-wordpress</loc> 
    <lastmod>2022-09-27</lastmod> 
    <xhtml:link rel="alternate" hreflang="en" href="https://newfd.jcogs.net/en/vps-wordpress"> </xhtml:link>
</url>
<url> 
    <loc>https://newfd.jcogs.net/pt-pt/vps-wordpress</loc> 
    <lastmod>2022-09-27</lastmod> 
    <xhtml:link rel="alternate" hreflang="pt-pt" href="https://newfd.jcogs.net/pt-pt/vps-wordpress"> </xhtml:link>
</url>
#7

Gavin @ JCOGS

The client has activated translated URLs, and is generating the translated urls manually, so I cannot rely upon auto-translation to generate, so I need to list the actual paths with url_titles as entered.

#8

BoldMinded (Brian)

That is what the translate_url function does. It looks at the site_pages array, or template names and paths, and url_titles to determine what the URL is for the non-default language. If the entry is in Structure, then it should return what you expect when using page_url.

Have you tried that code? If so did it not return what you expected?

#9

BoldMinded (Brian)

I don’t know what relative_url is but I think I made a mistake earlier, it should be

{exp:publisher:translate_url url=’{page_url}’ to_lang_id=”{id}”}

#10

Gavin @ JCOGS

Using either &#123;exp:publisher:translate_url url=’&#123;relative_url&#125;’ to_lang_id=”&#123;id&#125;”&#125; or &#123;exp:publisher:translate_url url='&#123;relative_url&#125;' to_lang_id='&#123;id&#125;' from_lang_id='&#123;publisher:current_language_id&#125;'&#125; in code from before gives

<url> 
    <loc>https://newfd.jcogs.net/Informacion/llamada-solicitada</loc> 
    <lastmod>2023-04-28</lastmod> 
    <xhtml:link rel="alternate" hreflang="en" href="https://newfd.jcogs.net/Informacion/llamada-solicitada"> </xhtml:link>
</url>

So no change.

#11

Gavin @ JCOGS

{page_url} is actually where we started with this. Unfortunately this code

<url>
        <loc>{exp:publisher:translate_url url='{page_url}' to_lang_id='{id}' from_lang_id='{publisher:current_language_id}'}</loc>
        <lastmod>{edit_date format='%Y-%m-%d'}</lastmod>
        <xhtml:link rel="alternate" hreflang="{short_name}" href="{exp:publisher:translate_url url='{page_url}' to_lang_id='{id}' from_lang_id='{publisher:current_language_id}'}" >
    </url>

generates this output …

<url> 
    <loc>https://newfd.jcogs.net/Informacion/politica-de-uso-aceptable</loc> 
    <lastmod>2022-09-27</lastmod> 
    <xhtml:link rel="alternate" hreflang="en" href="https://newfd.jcogs.net/Informacion/politica-de-uso-aceptable"> </xhtml:link>
</url>

i.e. same as before. {relative_url} is defined here: https://docs.expressionengine.com/v6/channels/entries.html#relative_url

#12

BoldMinded (Brian)

Maybe it’s just not going to work that way since it’s inside the languages loop. Backing up a bit… Based on my experience each language should have it’s own sitemap url, not everything in the same file… could you approach it from that direction?

#13

Gavin @ JCOGS

Interesting - not a good enough SEO wonk to know about one vs. many sitemaps. Will ask tame expert next time I’m in contact. Sounds like I just need to knuckle down and build a template that generates the URLs by bolting together components of the path - the site, slug and path are all known. Pity, but probably quicker in long run. Thanks for looking into this for me.

Login to reply