Exclude page template from sitemap

Hi there!
I’m trying to exclude from my sitemap all the page that have a specific template.

Here my snippet:
(the template that I want to exclude is “form-item”

<?= '<?xml version="1.0" encoding="utf-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <?php foreach ($pages as $p): ?>
    <?php if($p->template() !== 'form-item'): ?>
      <?php if(!strpos($p->url(), '/orders')): ?>
        <?php if (in_array($p->uri(), $ignore)) continue ?>
            <loc><?= html($p->url()) ?></loc>
            <lastmod><?= $p->modified('c', 'date') ?></lastmod>
            <priority><?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?></priority>
            <template> <?= $p->intendedTemplate() ?></template>
      <?php endif ?>
    <?php endif ?>
  <?php endforeach ?>

  <?php foreach ($pages->filterBy('template', 'not in', ['form-item']) as $p): ?>

Then remove the if statement.

If the template doesn’t exist, use intendedTemplate instead.

The reason why your if statement doesn’t work is because you are comparing a template object to a string. it would work like this:

    <?php if($p->template()->name() !== 'form-item'): ?>
1 Like