WordPress: Site Map

Creating a site map with WordPress is very simple. You might find it very handy especially if you’re running your whole site on WordPress, because this site map is automatic.

Firstly, you will need to have Exec PHP or PHP Execution installed and activated so you can use PHP within your pages.

Create a new page with whatever title you like.

Using the HTML editor, paste the following code:

<ul>
<?php wp_list_pages('title_li='); ?>
</ul>

This will display a site map of all the pages on your website in a bulleted list. By default, all published pages are displayed with no limit to depth and basically, no restrictions. They are also displayed in a hierarchical fashion in alphabetical order by page title.

It’s as simple as that. However, if you’re looking into customising, read ahead.

Customisation

There are many parameters which can be seen at the Codex but I have chosen to only display commonly used ones and how to use them.

sort_column

If you’re not liking the default alphabetical order, you can change your code by adding the parameter in bold:

<ul>
<?php wp_list_pages('title_li=&sort_column='); ?>
</ul>

By using the ampersand (&), you are adding another parameter along with the current one title_li.

sort_column has several variables. You can place these after the equals sign to have them take effect:

For example, this would be your code if you wanted to sort by the post date:

<ul>
<?php wp_list_pages('title_li=&sort_column=post_date'); ?>
</ul>

sort_order

If you aren’t liking the order of the pages and would like to reverse them, use the following code:

<ul>
<?php wp_list_pages('title_li=&sort_order=desc'); ?>
</ul>

Because, by default, the list is ascending. Don’t forget that if you have chosen to change other parameters, you add them along with others using &:

<ul>
<?php wp_list_pages('title_li=&sort_column=post_modified&sort_order=desc'); ?>
</ul>

exclude

Hopefully from reading a bit of the above you’re familiar with how to put the variables together.

exclude allows pages of specified IDs to be excluded from the list. For example, exclude=3,4,56 would exclude pages with the IDs 3, 4, and 56.

include

This is similar to exclude. Simply type in the page IDs of the pages you would like to be included.

For example, include=3,4,56 would include ONLY pages with the IDs 3, 4, and 56.

depth

This parameter controls the levels in the list of pages generated. By default, pages are displayed so that child pages (pages under a parent page) are indented using a nested list. However, this can be changed with the depth parameter and the following variables:

child_of

This only displays the child pages of a parent page, for instance, the written book reviews listed under a main “book reviews” page. Use as child_of=5, where the number 5 represents the page ID of the parent page.

show_date

This shows the date of the page (next to its name) when it was created or modified. By default no dates are displayed.

date_format

By default, if you choose to use show_date above, the date will display the way you have specified in your WordPress options – under Settings, General. This parameter allows you to change it using variables at php.net/date, for example:

<ul>
<?php wp_list_pages('title_li=&show_date=modified&date_format=l, F j, Y'); ?>
</ul>