WordPress HTML sitemap without a plugin

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

Being a WordPress user of many years I’ve found not much can go wrong with the open source CMS but when you have too many plug-ins it can be hard to keep them up to date. Where possible I always try to build a site which wont rely on them the main reasons for this is.

  • Better Security as plug-ins are the main source of website hacks
  • There is no guarantee plug-ins will be maintained or updated when a new version of WordPress comes out
  • They clog up the admin
  • They don’t always work exactly as you would like

Here is a bit of code I use to build custom HTML Sitemaps. You could even modify it to generate an XML sitemap too with a bit of work.

All you have to do it copy the following code into the functions.php file and it will add a HTML Sitemap short code to your site. Just put [htmlSitemap] on a page you want it to appear.

Put the following code in your themes functions.php file
    function get_html_sitemap( $atts ){

            $return = '';
            $args = array('public'=>1);
// If you would like to ignore some post types just add them to the array below
            $ignoreposttypes = array('attachment');

            $post_types = get_post_types( $args, 'objects' ); 

            foreach ( $post_types as $post_type ) {
                if( !in_array($post_type->name,$ignoreposttypes)){
                    $return .= '<h2>' . $post_type->labels->name.'</h2>';
                    $args = array(
                        'posts_per_page'   => -1,
                        'post_type'        => $post_type->name,
                        'post_status'      => 'publish'
                    $posts_array = get_posts( $args ); 
                    $return .=  '<ul>';
                    foreach($posts_array as $pst){
                        $return .=  '<li><a href="'.get_permalink($pst->ID).'">'.$pst->post_title.'</a></li>';
                    $return .=  '</ul>';

        return $return;
    add_shortcode( 'htmlSitemap', 'get_html_sitemap' );

Posted by Adi on January 24, 2016

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

8 responses to “WordPress HTML sitemap without a plugin”

  1. Kalli says:

    This is really helpful, thanks for that. Is there any way to preserve page hierarchy rather than just a flat list with all of the pages?

  2. Richard Bland says:

    This is a stunning bit of code. Just trying to figure out now how to ignore certain custom post types I have lol

    Thank you

  3. Adi says:

    Just add the post types you want to ignore to the array on this line

    $ignoreposttypes = array(‘attachment’);

  4. How to limiting the post at 500 early posts only

  5. Jana says:

    So useful! Thank you. How can I sort alphabetically?

  6. this is great tutorial thank you

  7. Ren says:

    Just what I needed thanks so much!!

  8. Kirill says:

    Thankksssss so much

Leave a Reply

Your email address will not be published. Required fields are marked *