How to build a dynamic sitemap for wordpress

Posted on 14th October, 2013 181 Comments

I always like to include a built in sitemap page to my themes (without using a plugin). So thought I’d include this quick tut on how this is achieved. Also to give the end user some control over whether this should be displayed, I’m including this as a template, for use with WP pages.

This template includes:

  • A Search field
  • Links to our feeds
  • Categories as a list
  • Archive section
  • Lists all Pages
  • Lists all Posts
  • A list of Custom Post Type posts

An example of this page can be found here

I’ll be adding the examples back soon.

Let’s begin by adding the usual template definition.

 * Template Name: Sitemap Template
 * @package WPtricks
<!-- All other content - below this line -->

Then save this as sitemap.php.

By defining this as a template we’ll be able to select it when we create a new page in the dropdown under the Page Attributes module on the right of “Add New Page”.

Now we have our template defined lets include the default page content: header, footer, page content.
I’m going to make this page full width, without the sidebar. If you’d like to include the sidebar just uncomment that below!

<?php get_header(); ?>

   <div id="sitemap" class="content-area">
   <div id="content" class="site-content" role="main">

         <?php while ( have_posts() ) : the_post(); ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

			<header class="entry-header">
                  <h1 class="entry-title h1"> <?php the_title(); ?> </h1> <!-- Include the page title -->

              <div class="entry-content">

				<?php the_content(); ?> <!-- Show the content added to the page by the user -->

                 <div class="sitemap">

					<!-- More content to come || Add below -->

					<!-- End here -->

                 </div><!-- .sitemap -->
			  </div><!-- .entry-content -->
			</article><!-- #post -->

		 <?php endwhile; // end of the loop. ?>

   </div><!-- #content -->
   </div><!-- #sitemap -->

<?php   /* Uncomment to show the sidebar
		get_sidebar();  */ ?>
<?php get_footer(); ?> <1-- Include the Footer -->

Now lets start to add our actual sitemap.

We’ll add all this content below the <!– More content to come || Add below–>:

Add a search box:

<section class="q-search">
<h2 class="entry-title">Quick Search</h2>
<?php get_search_form(); ?>

Add links to your site feeds

<section class="feeds">
<h2 class="entry-title">Feeds</h2>
	<li><a title="Full content" href="feed:<?php bloginfo('rss2_url'); ?>">Main RSS</a></li>
	<li><a title="Comment Feed" href="feed:<?php bloginfo('comments_rss2_url'); ?>">Comment Feed</a></li>

Show a list of our Categories

<section class="categories">
<h2 class="entry-title"><i class="icon-th-list"></i> Categories</h2>
   <ul><?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0&feed=RSS'); ?></ul>

Show the Archives

<section class="archives">
<h2 class="entry-title">Archives</h2>
	<?php wp_get_archives('type=monthly&show_post_count=true'); ?>

List the Pages

<section class="pages">
<h2 class="entry-title">Pages</h2>
	<ul><?php wp_list_pages("title_li="); ?></ul>

Show 10 Blog Posts

<section class="posts">
<h2 class="entry-title"><i class="icon-paper-clip"></i> All Blog Posts:</h2>
                  <ul><?php // Change showposts= to the amount you'd like to show on the front end.
		  $archive_query = new WP_Query('showposts=10');
                  while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                  <?php endwhile; ?>

Show the Custom Post Types as a list

In the example below I have a CPT called products so would like to show 10 posts from this post type.

<section class="cpt-posts">
<h2 class="entry-title">Products:</h2>
               	<ul><?php query_posts( array( 'post_type' => 'product', 'showposts' => 10 ) ); // Change product to the slug used by your CPT!
  					if ( have_posts() ) : while ( have_posts() ) : the_post();?>
                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
					<?php endwhile; endif; wp_reset_query(); ?>

Finally you’ll need to style the sitemap with some CSS. Below are some simple styles that you’ll be able to work from.

Add the Styles

#sitemap #content {
	width: 100%;

#sitemap section {
	width: 50%;
	float: left;
	display: block;
	margin: 1.3em 0 0 0;

#sitemap .q-search {}
#sitemap .feeds {}
#sitemap .categories {}
#sitemap .archives {}
#sitemap .pages {}
#sitemap .posts {}
#sitemap .cpt-posts {}

Now save up and add this to your theme folder, then you’re done.


To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

Buy Phyloday online

16th, Aug, 20

I do not even know how I ended up here, but I thought this post was good.
I don’t know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!


09th, Oct, 20

Wow all kinds of awesome advice! things to write a compare and contrast essay on [url=]essays writing services[/url] dissertation team


10th, Oct, 20

Many thanks! Great stuff. describe yourself college essay [url=]online dissertation[/url] ma dissertation


13th, Oct, 20

Thanks a lot, Fantastic stuff. canadian pharmacy viagra brand [url=]online pharmacy india[/url] drugs from canada online


13th, Oct, 20

Reliable facts. With thanks!
canadian pharmacy king [url=]canadian pharmacies online[/url] canadadrugs pharmacy


14th, Oct, 20

You actually explained this superbly! exemplary college essays [url=]how to write an argument essay[/url] phd dissertation help


14th, Oct, 20

With thanks. An abundance of postings!
writing a synthesis essay [url=]homework hotline[/url] write my thesis


15th, Oct, 20

Kudos! Numerous data!
pharmacy discount [url=]canadian pharmacies shipping to usa[/url] canadadrugs pharmacy


16th, Oct, 20

Whoa quite a lot of terrific data. get prescription online [url=]canadian pharmacies-24h[/url] prescription price comparison


17th, Oct, 20

Appreciate it, Plenty of material!
steps of writing an essay [url=]homeworks help[/url] thesis dissertation writing


18th, Oct, 20

Effectively expressed genuinely! . canadian pharmacy generic viagra [url=]canada online pharmacy[/url] canada pharmacies online


20th, Oct, 20

real canadian pharmacy: pain meds online without doctor prescription [url=]canadian pharmacy certified canada pharmacy online[/url] online pharmacy


22nd, Oct, 20

is high of the verdict of this usually intractable diagnostic. [url=]cialis cialis[/url] expression some weeks outdo role to allow cialis online forum.

viagra boys

26th, Oct, 20

Buy Cheap cialis impotence drug eli lilly co Online Top Online Pharmacy.

Cheap Pharmacy Online. viagra boys Buy Cheap cialis impotence drug
eli lilly co Online Top Online Pharmacy. Cheap Pharmacy

Life Experience Degree

26th, Oct, 20

Hello there, I believe your website may be having web browser compatibility issues. When I take a look at your web site in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads up! Aside from that, excellent blog!


03rd, Nov, 20

can erectile dysfunction be corrected [url=]erectile dysfunction pills[/url] best erectile dysfunction pumps


03rd, Nov, 20

erectile after you quit drinking [url=]erectile pills canada[/url] best erectile medication


06th, Nov, 20

Regards! Lots of knowledge.
how to write a synthesis essay [url=]phd thesis help[/url] custom essay writing service


06th, Nov, 20

what is a high blood pressure reading
[url=]online pharmacies[/url]


06th, Nov, 20

Many thanks! Plenty of data!
online custom essay writing service [url=]courses work[/url] uk dissertation writing services


08th, Nov, 20

Cheers. Fantastic stuff. custom essays toronto [url=]define dissertation[/url] tourism dissertation


09th, Nov, 20

Wow plenty of wonderful info! issues to write about in a college essay [url=]doctoral dissertation[/url] buy a dissertation


09th, Nov, 20

department oh health survery [url=]kamagra oral jelly[/url] Ukzea14 qffvet


09th, Nov, 20

lawrence county ohio health department missouri department of social services definition of ambulatory mental health providers definition buy viagra without seeing doctor [url=]viagra without a doctor prescription[/url] ekxgcj


11th, Nov, 20

You’ve made your point extremely well.. why do you want to go to college essay [url=]homework now[/url] dissertation editing


12th, Nov, 20

Valuable tips. Cheers. critical essay help [url=]help in thesis writing[/url] define disseration


13th, Nov, 20

[url=]medication for ed[/url] – canadian drugs
[url=]zithromax canadian pharmacy[/url] – zithromax online no prescription
[url=]lasix medication[/url] – lasix pills


14th, Nov, 20

Useful content. Thank you..
[url=]cialis online[/url]


17th, Nov, 20

Nicely spoken genuinely! !

What Time Of Day To Take Provigil [url=]provigil lawsuit[/url]


19th, Nov, 20

You actually stated this fantastically! cheap essay writing service [url=]resume writing services[/url] example dissertation


26th, Nov, 20

[url=]cialis generic[/url] [url=]viagra without doctor prescription[/url] [url=]finasteride[/url]


29th, Nov, 20

[url=]tadalafil[/url] [url=]viagra without doctor prescription[/url] [url=]finasteride[/url]


30th, Nov, 20

Healthcare continues to be an notable, but divisive public topic. Not surprisingly, 41 percent of available voters said healthcare was their critical climax in the mid-term elections in 2018.viagra without a prior doctor prescription In 2008, when the ACA became law, merely 46 percent of voters supported single payer healthcare. That army has grown significantly to 59 percent approval in beginning 2018.[url=]cialis on sale online[/url] While Medicare-for-all legislation is unlikely to pass both the Bordello and Senate in its current mould, there is a switch manage in obvious opinion with a dynamic the greater part now in favor.


03rd, Dec, 20

[url=]buy cialis[/url] [url=]viagra generic[/url] [url=]finasteride[/url]

find this electrician free estimate

03rd, Dec, 20

Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]