Wednesday, 05th Apr 2017

«

»

Print this Post

How to create an HTML sitemap in WordPress

An HTML sitemap makes it easier for users to find content in your site. If you visit my archives page, you can see a list of my published posts under each category. There are plenty of plugins to create such sitemaps in a WordPress site. But with a bit of php code, you can create an HTML sitemap in WordPress yourself and have more control over the output.

Add the below php function to your theme (or child-theme) functions.php file after the opening <?php php tag. If you think hacking up theme files is beyond your skills, you can download a simple plugin at the end of this post. But hey, I still recommend you to play with this function! Remember what I said earlier? There are plenty of such plugins. I don’t want this to be ended up as yet another plugin.

function spp_html_sitemap() {
 $spp_sitemap = '';
 $published_posts = wp_count_posts('post');
 $spp_sitemap .= '<h4 id="sitemap-posts-h4">Here is a list of of my '.$published_posts->publish.' published posts</h4>';
 
 $args = array(
  'exclude' => '', /* ID of categories to be excluded, separated by comma */
  'post_type' => 'post',
  'post_status' => 'publish'
  );
	$cats = get_categories($args);
	foreach ($cats as $cat) :
	$spp_sitemap .= '<div>';
	$spp_sitemap .= '<h3>Category: <a href="'.get_category_link( $cat->term_id ).'">'.$cat->cat_name.'</a></h3>';
	$spp_sitemap .= '<ul>';
 
  query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
  while(have_posts()) {
    the_post();
    $category = get_the_category();
    /* Only display a post link once, even if it's in multiple categories */
    if ($category[0]->cat_ID == $cat->cat_ID) {
    	$spp_sitemap .= '<li class="cat-list"><a href="'.get_permalink().'" rel="bookmark">'.get_the_title().'</a></li>';
 
    }
  }
  $spp_sitemap .= '</ul>';
  $spp_sitemap .= '</div>';
 
  endforeach;
 
  $pages_args = array(
	'exclude' => '', /* ID of pages to be excluded, separated by comma */
	'post_type' => 'page',
	'post_status' => 'publish'
	); 
 
  $spp_sitemap .= '<h3>Pages</h3>';
  $spp_sitemap .= '<ul>';
  $pages = get_pages($pages_args); 
  foreach ( $pages as $page ) :
  $spp_sitemap .= '<li class="pages-list"><a href="'.get_page_link( $page->ID ).'" rel="bookmark">'.$page->post_title.'</a></li>';
  endforeach;
  $spp_sitemap .= '<ul>';
 
 return $spp_sitemap;
}
add_shortcode( 'spp-sitemap','spp_html_sitemap' );

This lists all posts and pages. Posts are sorted category-wise. A post will be displayed once, even if it is published under multiple categories. You can exclude certain pages and categories by entering their ID. (See comment in code.) If you don’t want to display pages, remove the following block of code from the function,

  $pages_args = array(
	'exclude' => '', /* ID of pages to be excluded, separated by comma */
	'post_type' => 'page',
	'post_status' => 'publish'
	); 
 
  $spp_sitemap .= '<h3>Pages</h3>';
  $spp_sitemap .= '<ul>';
  $pages = get_pages($pages_args); 
  foreach ( $pages as $page ) :
  $spp_sitemap .= '<li class="pages-list"><a href="'.get_page_link( $page->ID ).'" rel="bookmark">'.$page->post_title.'</a></li>';
  endforeach;
  $spp_sitemap .= '<ul>';

After that, add [spp-sitemap] shortcode anywhere to display the sitemap.

Plugin

I made a WordPress plugin using this function and hosted it on GitHub. Fork and play with it. Pull requests are welcome! Download the plugin here.

Download SPP HTML Sitemap

Short Link:

About the author

Prasanna SP

Student | WordPress Dev | Tech Blogger | Interested in GNU/Linux, FOSS, PHP, Drupal, WordPress, Ethical hacking, Photography, Painting, Literature etc..

3 comments

  1. Yael Shahar

    This is wonderful! Works like a charm. Now I just have to figure out a way to make a tag index and I’m all set!

  2. Aryan Arya

    can you tell me how many time we have to submit sitemap to Google webmaster tools?
    I mean is this a necessary to submit sitemap every time when you write a new post to be indexed in Google.

  3. Manjunath Naik

    Nice article, Using Yoast seo plugin also u can make effective Sitemap.

Leave a Reply

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

Please put your code snippet between <code> and </code>. Comment moderation is in use. Please do not submit your comment twice -- it will appear shortly.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close