Collective Idea

Collective Idea Logo

Daniel Morrison

sitemap.xml in Harmony

By Daniel Morrison on June 14, 2010 in code, harmony, sitemap, and xml

Updated 7-11-2011: Found a better way to do this so updated the example below.

We love that our new site is powered by Harmony but I didn’t love not having a sitemap.xml.

Turns out its pretty easy to do.

First, build a new template. I called mine sitemap.xml.tpl

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% include 'sitemap_page' for site.root %}

  {% for post in site.recent_posts.10000 %}
    {% include 'sitemap_page' for post %}
  {% endfor %}
</urlset>

This sets up the basic XML and then calls an include with the homepage (site.root).

The include file looks like this (mine is named sitemap_page.inc)

<url>
  <loc>{{sitemap_page.url}}</loc>
  <lastmod>{{ sitemap_page.updated_at | date: '%Y-%m-%d' }}</lastmod>
  <changefreq>daily</changefreq>
  <priority>{{sitemap_page.ancestors | size | times: -0.1 | plus: 1.0 }}</priority>
</url>

{% for child in sitemap_page.navigation %}
  {% include 'sitemap_page' for child %}
{% endfor %}

The first section is the xml that for the current page. Unfortunately, there isn’t a good way to get the time for the <lastmod> element (google doesn’t like -0400 and wants -04:00 instead. bah!)

I should really change the <chagefreq>, but I’m lazy for now.

The priority is a bit clever. I basically take the depth of the page and decrement the priority by 0.1. The homepage is 1.0, portfolio is 0.9, and a portfolio item is 0.8. Won’t work for everybody, but does for me.

Next, we recursively loop through each subpage. I use sitemap_page.navigation so that it only shows pages included in navigation. For example, every person in our About section is technically a different page, but we pull them all into the about page. By taking them out of the navigation, I don’t link to non-existant pages.

Then, I look over recent posts if there’s a blog. We only have one, but it will work with as many as you have. I also don’t know of a way to get all posts (Harmony team, please enlighten me) so I’m grabbing 10,000. That’ll do for a while.

To get it to show on the site, I simply make a top-level page with the permalink of sitemap.xml and tell it to use my sitemap template.

The result is a sitemap.xml file that Google likes and indexes. check it out!

By Daniel Morrison on June 14, 2010 in code, harmony, sitemap, and xml

3 Comments

  1. John Nunemaker

    John Nunemaker June 14, 2010 http://orderedlist.com

    Nice! Note: even though you put 10,000 it will only grab 200 right now. The main reason is grabbing that many posts is a lot. I’m sure we’ll bump up from 200 soon and someday we’ll try to remove the limit.

  2. Daniel Morrison

    Daniel Morrison June 14, 2010 http://collectiveidea.com

    John Nunemaker: Hm, ok. That’s good to know. Maybe you should build in sitemap functionality ;)

  3. Nick

    Nick March 02, 2012 http://www.kykloud.com

    Thanks for this – very useful.
    Harmonyapp should do this out the box!

Post a Comment

Contact Us

Find us on Google Maps
Collective Idea
25 West 8th Street, Suite 200
Holland, Michigan 49423 USA 42.790299-86.108415

Follow us on the Interwebs

We are currently available for medium and long term projects. Please get in touch if we can be of service.