364-sitemaps.md 1.52 KB
Newer Older
1
Title: Google Sitemaps and WordPress 2.0
2
Slug: 364/sitemaps
3
Summary: Quick and dirty.
4 5
Date: 2006-03-30 17:06
Author: Adam
6 7
Category: Meta
Tags: WordPress
8 9
WordPress-Post-ID: 364
WordPress-Post-Type: post
10 11 12 13 14 15

I've created a script to dump my blog post history into an XML file
suitable for [Google Sitemaps][]. Note that I consider any XML-building
script that does not use [XML creation tools][] to be a hack. This is a
hack.

16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    <?php

    header("Content-type: application/xml");
    require_once( dirname(__FILE__) . '/wp-config.php'); // get the wordpress functions

    // sitemap header
    echo '<'.'?xml version="1.0" encoding="UTF-8"?>';
    echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">';

    // set upper limit to a sufficiently high number
    $posts = get_posts('numberposts=32767&order=ASC&orderby=post_date');

    // output all the posts
    foreach($posts as $post): setup_postdata($post);
    ?>
        <url>
            <loc><?php the_permalink(); ?></loc>
            <lastmod><?php echo get_post_modified_time('Y-m-d') ?></lastmod>
        </url>
    <?php

    endforeach;
    echo '</urlset>';
40

41

42 43 44 45 46

Example: [sitemap.php][].

I also add a rewrite condition to my `.htaccess` file:

47

48 49
    RewriteRule ^sitemap.xml$ sitemap.php [L]

50

51 52 53 54 55 56 57

Thus is born [sitemap.xml][].

  [Google Sitemaps]: http://www.google.com/webmasters/sitemaps/
  [XML creation tools]: http://us2.php.net/manual/en/ref.domxml.php
  [sitemap.php]: http://blogs.bwerp.net/~adam/2006/03/30/sitemap.phps
  [sitemap.xml]: http://blogs.bwerp.net/sitemap.xml