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.

<?php
/*
 * 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 -->
            </header>

              <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(); ?>
</section>

Add links to your site feeds

<section class="feeds">
<h2 class="entry-title">Feeds</h2>
<ul>
	<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>
</ul>
</section>

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>
</section>

Show the Archives

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

List the Pages

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

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(); ?>
                  <li>
                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                  </li>
                  <?php endwhile; ?>
                  </ul>
</section>

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();?>
					<li>
                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                  </li>
					<?php endwhile; endif; wp_reset_query(); ?>
                 </ul>
</section>

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,
#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.

Comments

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!

Durekbah

09th, Oct, 20

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

Durekbah

10th, Oct, 20

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

DwayneDethy

13th, Oct, 20

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

MelviFug

13th, Oct, 20

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

Durekbah

14th, Oct, 20

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

Direkbah

14th, Oct, 20

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

MelviFug

15th, Oct, 20

Kudos! Numerous data!
pharmacy discount [url=https://canadianpharmaciesmsn.com/]canadian pharmacies shipping to usa[/url] canadadrugs pharmacy

DwayneDethy

16th, Oct, 20

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

Durekbah

17th, Oct, 20

Appreciate it, Plenty of material!
steps of writing an essay [url=https://quality-essays.com/]homeworks help[/url] thesis dissertation writing

DwayneDethy

18th, Oct, 20

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

Kevenbes

20th, Oct, 20

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

JamesVen

22nd, Oct, 20

is high of the verdict of this usually intractable diagnostic. [url=https://ciamedusa.com/]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
Online.

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!

RobertBAR

03rd, Nov, 20

can erectile dysfunction be corrected [url=http://erectiledysfunctionpillscvs.com/#]erectile dysfunction pills[/url] best erectile dysfunction pumps

RobertBAR

03rd, Nov, 20

erectile after you quit drinking [url=http://erectiledysfunctionpillscvs.com/#]erectile pills canada[/url] best erectile medication

Durekbah

06th, Nov, 20

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

HarryCloth

06th, Nov, 20

what is a high blood pressure reading
[url=http://canadianpharmacy-md.com]online pharmacies[/url]

Direkbah

06th, Nov, 20

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

Durekbah

08th, Nov, 20

Cheers. Fantastic stuff. custom essays toronto [url=https://service-essay.com/]define dissertation[/url] tourism dissertation

Direkbah

09th, Nov, 20

Wow plenty of wonderful info! issues to write about in a college essay [url=https://quality-essays.com/]doctoral dissertation[/url] buy a dissertation

ytjbkgi

09th, Nov, 20

department oh health survery [url=https://www.kamagramama.com]kamagra oral jelly[/url] Ukzea14 qffvet

oaahbqz

09th, Nov, 20

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

Durekbah

11th, Nov, 20

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

Direkbah

12th, Nov, 20

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

DavidBat

13th, Nov, 20

[url=https://edpillscanada24.com/#]medication for ed[/url] – canadian drugs
[url=https://zithromax10.com/#]zithromax canadian pharmacy[/url] – zithromax online no prescription
[url=https://lasixtop.com/#]lasix medication[/url] – lasix pills

JesMox

14th, Nov, 20

Useful content. Thank you..
[url=https://www.cialiset.com]cialis online[/url]

BrianTat

17th, Nov, 20

Nicely spoken genuinely! !

What Time Of Day To Take Provigil [url=https://topmodafinilrxpills.com/]provigil lawsuit[/url]

Durekbah

19th, Nov, 20

You actually stated this fantastically! cheap essay writing service [url=https://discountedessays.com/]resume writing services[/url] example dissertation

UMaf

26th, Nov, 20

[url=https://www.cialiset.com/]cialis generic[/url] [url=https://www.winviag.com/]viagra without doctor prescription[/url] [url=https://www.finasteridewin.com/]finasteride[/url]

IMaf

29th, Nov, 20

[url=https://www.cialis20walmart.com/]tadalafil[/url] [url=https://www.withoutvisit.com/]viagra without doctor prescription[/url] [url=https://www.proscarhair.com/]finasteride[/url]

Tietty

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=http://my-canadianpharmacyonline.com]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.

YMaf

03rd, Dec, 20

[url=https://www.cialis20walmart.com/]buy cialis[/url] [url=https://www.withoutvisit.com/]viagra generic[/url] [url=https://www.finasteridewin.com/]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]