sitemap.xml in Harmony

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

  {% include 'sitemap_page' for site.root %}

  {% for post in site.recent_posts.10000 %}
    {% include 'sitemap_page' for post %}
  {% endfor %}

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)

  {{sitemap_page.url}}
  {{ sitemap_page.updated_at | date: '%Y-%m-%d' }}
  daily
  {{sitemap_page.ancestors | size | times: -0.1 | plus: 1.0 }}


{% 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 </code> element (google doesn't like -0400 and wants -04:00 instead. bah!)

I should really change the </code>, 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!

Photo of Daniel Morrison

Daniel founded Collective Idea in 2005 to put a name to his growing and already full-time freelance work. He works hard writing code, teaching, and mentoring.

Comments

Add a Comment

Hmm...that didn't work.

Something went wrong while adding your comment. If you don't mind, please try submitting it again.

Comment Added!

Your comment has been added to this post. Please refresh this page to view it.

Optional. If added, we will display a link to the website in your comment.
Optional. Never shared or displayed in your comment.
  1. June 14, 2010 at 21:50 PM

    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. June 14, 2010 at 23:27 PM

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

  3. March 02, 2012 at 11:32 AM

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