23 thoughts on “How to Build the Perfect WordPress Sitemap Leave a comment

  1. I use the Google Sitemap Generator plugin. Can this code still be implemented, or would that be redundant?

    1. These are for two different things. The Sitemap Generator plugin is used to generate xml sitemaps whereas the code I used it for displaying html sitemap pages on your website.

      BTW I changed your name to something resembling your real name 🙂

  2. This is terrific. I also need to generate a Site Map where the Posts are listed by Category rather than date. Basically, I want to build a site map that reads like a Table of Contents for Posts … where posts are listed alphabetically by title within their category, and reflecting the category hierarchy. Any ideas? Thanks!

  3. This article feels a bit half-finished, unless I’ve missed something. Once the code is added to functions.php, how do you display the sitemap on a page?

    1. Hi Joe – thanks for the feeback – I have added some extra notes to the bottom of the post to try to clarify what to do with the function.

  4. Hi Ben,

    Thanks for sharing this, I think your sitemap looks great. I’ve added the function to my functions.php and called it in my page temple. Unfortunately it’s not outputting anything at all!

    This is how I called the function…

    Is there any reason that your code isn’t working for me?


  5. great tutorial and got it all working quite easily, however, unlike your sitemap on Binary Moon, this only outputs posts and not pages. Anyway to amend so that it generates page links in the sitemap?

    I’m only just getting my head around WordPress but know a bit of PHP and SQL and have tried altering your SQL statement:
    AND post_type=”post”


    AND post_type=”page”

    however it still generates a map based on pages. Is it something to do with the_post() function?

    thanks again though!

  6. And what about displaying posts and pages together? Is there an easy way to adapt your code to do that?

  7. Hi Ben. Hopefully everything is good with you. I have paste the code given to my function.php but some of the script appear on top of my page header. Can you tell what I did wrong?

  8. Hi Ben,

    I followed your advice and got my “sitemap” up and running…
    Just wanna say thxs


  9. Hello Ben,
    I have an HTML based sitemap but it seems like Google don’t like them much. May .xml based sitemap would be more appropriate. Any idea? Is there anyway I can create .xml sitemap without any PLUGIN? which won’t need manual update? I do blog nearly everyday. So, manual .xml sitemap gonna be a real boring thing to do. Please help. Thanks.

    1. My tutorial was for a public sitemap. If you want an automated system then the best thing to do is use a plugin – there’s a couple of good ones out there so just google around :).

  10. hello
    I tried your code and it works nicely but it sticks with the posts and I would like it to list only my custom posts (livres).
    So in the mysql query, i put AND post_type=’livres’
    but it keeps displaying regular posts instead of books !
    Should I modify more the query ?
    thank you

Leave a Reply

Your email address will not be published.