Please see WordPress Sitemap Class for the main write-up and code examples.
The class file is slightly different to cope with PHP 4 limitations, but with the same class features. There’s one main difference in it’s usage, and that it that methods can’t chain together:
require_once 'classes/GT_Sitemap.php';
$sitemap = new GT_Sitemap;
$sitemap->setOrder('posts', 'pages');
$sitemap->setDateFormat('');
$sitemap->setCustomPagesQuery('exclude=812');
$sitemap->shortcode('sitemap');
The Class File for PHP4
<?php
/**
* Allows XHTML sitemap to be added. Suitable for PHP 4.x
*
* @package GT_Sitemap
* @author Gary Jones
* @version 2010-04-26
* @since 2010-03-20
*/
class GT_Sitemap
{
/**
* @var string
*/
var $_pagesText;
/**
* @var string
*/
var $_postsText;
/**
* @var string
*/
var $_archivesText;
/**
* @var int
*/
var $_headingLevel;
/**
* @var array
*/
var $_order = array();
/**
* @var bool
*/
var $_showPageDate;
/**
* @var string
*/
var $_showPostDate;
/**
* @var bool
*/
var $_showPostCount;
/**
* @var string
*/
var $_archivesType;
/**
* @var string
*/
var $_pagesDateFormat;
/**
* @var string
*/
var $_postsDateFormat;
/**
* @var string
*/
var $_customPagesQuery = '';
/**
* PHP4 compatible constructor
*/
function GT_Sitemap() {
if(version_compare(PHP_VERSION,"5.0.0","__construct();
}
}
/**
* PHP5 constructor, setting defaults
*/
function __construct() {
$this->_pagesText = 'Pages';
$this->_postsText = 'Posts';
$this->_archivesText = 'Monthly Archives';
$this->_headingLevel = 3;
$this->_order = array('pages', 'posts', 'archives');
$this->_showPageDate = true;
$this->_showPostDate = 'published';
$this->_showPostCount = true;
$this->_archivesType = 'monthly';
$this->_pagesDateFormat = get_option('date_format');
$this->_postsDateFormat = get_option('date_format');
}
/**
* @param string $id
*/
function setPagesText($id) {
$this->_pagesText = $id;
return $this;
}
/**
* @param string $id
*/
function setPostsText($id) {
$this->_postsText = $id;
return $this;
}
/**
* @param string $id
*/
function setArchivesText($id) {
$this->_archivesText = $id;
return $this;
}
/**
* @param array $id
*/
function setOrder($arg1, $arg2 = null, $arg3 = null) {
$this->_order = func_get_args();
return $this;
}
/**
* @param int $id
*/
function setHeadingLevel($id) {
$this->_headingLevel = $id;
return $this;
}
/**
* @param string $id
*/
function setPageDateFormat($id) {
if ( 0 === func_num_args() ) {
$this->_showPageDate = '';
} else {
$this->_pagesDateFormat = $id;
}
return $this;
}
/**
* @param string $id
*/
function setPostDateFormat($id) {
if ( 0 === func_num_args() ) {
$this->_showPostDate = '';
} else {
$this->_postsDateFormat = $id;
}
return $this;
}
/**
* @param string $id
*/
function setDateFormat($id) {
$this->setPageDateFormat($id);
$this->setPostDateFormat($id);
return $this;
}
/**
*
*/
function hidePostCount() {
$this->_showPostCount = false;
return $this;
}
/**
* @param string $id
*/
function setArchivesType($id) {
$archiveTypes = array('yearly', 'monthly', 'daily', 'weekly', 'postbypost', 'alpha');
if ( in_array($id, $archiveTypes) ) {
$this->_archivesType = $id;
$this->setArchivesText(substr_replace($id, strtoupper(substr($id, 0, 1)), 0, 1) . ' Archives');
}
return $this;
}
/**
* @param string $id
*/
function setCustomPagesQuery($pagesQuery) {
$this->_customPagesQuery = $pagesQuery;
return $this;
}
/**
* @param string $shortcode The shortcode keyword that will be used to output the sitemap
*/
function shortcode($shortcode) {
add_shortcode( $shortcode, array(&$this, 'build') );
}
/**
* Does the main work of creating the output
*/
function build() {
foreach ($this->_order as $section) {
if ( 'pages' === $section ) {
$output .= '_headingLevel . '>' . $this->_pagesText . '_headingLevel . '>' . "\n"
. '<ul>' . "\n" . wp_list_pages('echo=0&show_date=' . $this->_showPageDate . '&date_format=' . $this->_pagesDateFormat . '&title_li=&' . $this->_customPagesQuery) . '</ul>' . "\n";
}
if ( 'posts' === $section ) {
$output .= '_headingLevel . '>' . $this->_postsText . '_headingLevel . '>'."\n"
. '<ul>' . "\n" . $this->_posts_by_category() . '</ul>' . "\n";
}
if ( 'archives' === $section ) {
$output .= '_headingLevel . '>' . $this->_archivesText . '_headingLevel . '>' . "\n"
. '<ul>' . "\n" . wp_get_archives('type=' . $this->_archivesType . '&echo=0&show_post_count=' . $this->_showPostCount). '</ul>' . "\n";
}
}
return $output;
}
function _posts_by_category() {
global $wpdb, $post;
$tp = $wpdb->prefix;
$sort_code = 'ORDER BY name ASC, post_date DESC';
$the_output = NULL;
$last_posts = (array)$wpdb->get_results("SELECT {$tp}terms.name, {$tp}terms.term_id, {$tp}term_taxonomy.term_taxonomy_id FROM {$tp}terms, {$tp}term_taxonomy WHERE {$tp}terms.term_id = {$tp}term_taxonomy.term_id AND {$tp}term_taxonomy.taxonomy = 'category'");
if (empty($last_posts)) {
return NULL;
}
$the_output .= '';
$used_cats = array();
$i = 0;
foreach ($last_posts as $posts) {
if (in_array($posts->name, $used_cats)) {
unset($last_posts[$i]);
} else {
$used_cats[] = $posts->name;
}
$i++;
}
$last_posts = array_values($last_posts);
foreach ($last_posts as $posts) {
$the_output .= '<li><a>term_id) . '"><strong>' . apply_filters('list_cats', $posts->name, $posts) . '</strong></a><ul>';
$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (SELECT object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id =" . $posts->term_taxonomy_id . ") ORDER BY post_date DESC");
foreach ( $arcresults as $arcresult ) {
$the_output .= '<li><a>ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a> ';
if ($this->_showPostDate) {
$the_output .= date($this->_postsDateFormat,strtotime($arcresult->post_date));
}
$the_output .= '</li>';
}
$the_output .= '</ul></li>';
}
return $the_output;
}
}
[...] only for PHP5 installations, and WILL cause errors if you have PHP4. Luckily, you can use the version for PHP4 instead [...]