Exclude entire subdirectory from sitemap

It looks like there are about 137 plugins for generating sitemaps, and not having a clue if any of them are better than any others, I’m just going with the standard sitemap template mentioned in the Kirby docs.

It’s really easy to exclude individual pages, but it doesn’t exclude entire directories.

So, questions:

  1. What is the easiest way with a collection of Kirby pages (in this case, the index) to exclude an entire subdirectory? I don’t want to dig too deep into PHP parsing if there’s an easy way already.
  2. If there’s a sitemap generating plugin available that makes #1 easier, I would be open to that.

The not() method now (since 2.3.0) accepts a collection, so you should be able do something like this:

$excluded = page('somepage')->index();
$index = $site->index()->not($excluded);
1 Like

@fitzage:
If you want to look at an older solution:
https://forum.getkirby.com/t/sitemaps-easiest-way/361/5
which runs with the actual Kirby version.

Look there at:

$ignore = array('sitemap', 'error');                       // pages with uri IN $ignore are ignored
$templateignore = array('internal', 'members' , 'member'); // pages with template IN $templateignore are ignored

… and the following using this vars.

[Added:]
You can use the same solution for a webpage with the html-sitemap, which shows the links to several of your (may be visible) webpages.

Good luck!

Thanks, I saw that before, but I’m not sure templates is going to give me what I need, either.

1 Like

Ooh, excellent. I’ll give that a try.

1 Like

Works brilliantly. Doesn’t exclude the parent page, but that can easily be added to the list of excluded pages.

1 Like

This also supports an array in the excluded variable, something like $excluded = page('somepage')->index(), page('someotherpage')->index()), so it’s perfect for what I need. Just have ->not($excluded) along with ->not($ignore) for the ignored pages, and it’s golden.

1 Like

You could also use a custom filter:

$index = $site->index()->not(array('error', 'contact'))->filter(function($page){
      return strpos($page->uri(), 'some_page') !== 0 && strpos($page->uri(), 'some_other_page') !== 0;
});

Only makes sense if the number of entire directories to exclude is rather limited, though.

1 Like

Could you please post you template? Thanks!

OK, here’s my template with the arrays simplified. https://gist.github.com/fitzage/4ff7690d2a6f7faf91b1bbe0a9367d15

Here’s what’s happening:

  1. I’m not eliminating invisible pages, because the current site structure uses some “invisible” pages as primary pages due to our weird navigation.
  2. I have not yet figured out how to have items in $ignoredirs ignore the parent, so you need to also include the parent in $ignore if you want that parent hidden. This can actually be handy sometimes, though, because I have a page that I want to show up, but the children are only there for being pulled into the main page, so I don’t want them to show in the sitemap because they won’t be linked to directly.
  3. The $numerouno array is specific pages that you want to explicitly set as the highest priority in the sitemap. Everything else is based on depth.
  4. You’ll notice I’m calling events separately. That’s because all other content, I only want included if the date isn’t in the future, but for events I obviously want to include future dates, as they’re the most important.
1 Like