Sitemap.xml returns error, due to language config

I want to create a sitemap, described here.

I tried the solution in that post, but got this error (while trying to open sitemap.xml from the browser);

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<b>Notice</b>
: Trying to get property of non-object in
<b> G:\xampp\htdocs\test-site\kirby\branches\multilang\page.php </b>
on line <b>70</b>
<b>Notice</b>
: Trying to get property of non-object in
...
...

I know it has something to do with the language-set in config.php (when I delete the code below, the sitemap works).

c::set('languages', array(
    array(
        'code'    => 'de',
        'name'    => 'Deutsch',
        'locale'  => 'de_DE',
        'default' => true,
        'url'     => '/'
    )
));

Is there any way I can get a proper sitemap.xml with my current language settings?

It looks like the script is looking for pages / files that do not exist?

I have files like \content\1-home\home.de.txt.

If the configuration code you use is the actual code, the reason might be that you forgot a closing quote:

c::set('languages', array(
    array(
        'code'    => 'de',
        'name'    => 'Deutsch',
        'locale'  => 'de_DE',
        'default' => true,
        'url'     => '/'
    )
));

Sorry, the missing quote was a typo - only made in the post (so it’s correct in config.php).

I double checked - but still get the error, while querying the sitemap.xml.

When I remove the language-config from config.php the sitemap.xml works perfect :frowning:

It throws errors on these lines;

kirby\branches\multilang\page.php #line 70
kirby\branches\multilang\page.php #line 199
kirby\branches\multilang\page.php #line 202

…for each page I have in my contents-directory.

Oh, I know why. You need to change the sitemap route and add the following line before calling the template:

site()->visit('sitemap', 'de');

This will set the current language to German so that Kirby knows where to get the data from.

That is the solution!

Just for the others, who encounters this same error (creating a sitemap, while having a language set in your config.php), this is the code in /site/templates/sitemap.php

site()->visit('sitemap', 'de');

$ignore = array('sitemap', 'error');

// send the right header
header('Content-type: text/xml; charset="utf-8"');

// echo the doctype
echo '<?xml version="1.0" encoding="utf-8"?>';

?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <?php foreach($pages->index() as $p): ?>
  <?php if(in_array($p->uri(), $ignore)) continue ?>
  <url>
    <loc><?php echo html($p->url()) ?></loc>
    <lastmod><?php echo $p->modified('c') ?></lastmod>
    <priority><?php echo ($p->isHomePage()) ? 1 : number_format(0.5/$p->depth(), 1) ?></priority>
  </url>
  <?php endforeach ?>
</urlset>

Is this feature (adding the first line before opening the template) somewhere documented? Did I overlook it? Or is it just you, being a genius?

Generally you wouldn’t add it to the template but to the route, that’s why it is documented in the Routing docs. You don’t need to use it for normal templates.

I cannot find

with and without the second string in the Cheat Sheet of the Kirby docs.

As @lukasbestle pointed out, it’s in the Routing docs, not in the Cheat Sheet.

Yes, you are as always right, but I would not search there, if I don’t know that place.
I would look at http://getkirby.com/docs/cheatsheet#site and I find nothing.
That was, what I have said.

I know that there is a difference between $site and site(), but I have looked at the wrong place.

May be someone wants to add this at site()

site()->visit() is a method that’s only useful in context of routing. I would never search for it specifically. That’s why it isn’t in the cheat sheet: To keep the cheat sheet clean so it can answer questions like “how can I get the URI of the page?” more quickly.