<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SofaRider &#187; site map</title>
	<atom:link href="http://rider.sofarider.com/tag/site-map/feed/" rel="self" type="application/rss+xml" />
	<link>http://rider.sofarider.com</link>
	<description>WordPress Themes, Widgets, Development &#124; WEB &#38; Graphic Design</description>
	<lastBuildDate>Wed, 14 Oct 2009 08:40:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress &#8211; simple site map code</title>
		<link>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/</link>
		<comments>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 06:41:49 +0000</pubDate>
		<dc:creator>Feeleep</dc:creator>
				<category><![CDATA[Wordpress Tips]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[site map]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://rider.sofarider.com/?p=284</guid>
		<description><![CDATA[Recently I&#8217;ve been proposed to add a sitemap template page (read functionality) to one of my WordPress themes. I started to Google for it but none of available plugins or code snippets didn&#8217;t do well. What my client actually wanted to have was something similar to a tree system where folders would represent categories/sub-categories and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rider.sofarider.com/wp-content/uploads/2009/09/tree-200x200.png" alt="tree" title="tree" width="200" height="200" class="alignnone size-thumbnail wp-image-283" />Recently I&#8217;ve been proposed to add a sitemap template page (read functionality) to one of my WordPress themes. I started to Google for it but none of available plugins or code snippets didn&#8217;t do well. What my client actually wanted to have was something similar to a tree system where folders would represent categories/sub-categories and files would &#8216;become&#8217; Post titles. So, here&#8217;s the result of a really simple site map generator for any WordPress theme.<br />
I do not claim it&#8217;s perfect and I&#8217;m aware it needs certain improvements but I guess it&#8217;s quite good for that purpose.</p>
<h2>The code</h2>
<p>Please note that I have created new WordPress template file and save it as &#8217;sitemap.php&#8217; in theme&#8217;s root folder. For more info regarding custom WP template files and how to use them, please refer to <a href="http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates" target="_blank">this page</a>.<br />
Also, another important info is that this code will work only for Custom Permalink structure set to <strong>/%category%/%postname%/</strong>.</p>
<pre>
<code>
&lt;?php
    // easy job for pages...
    echo '&lt;ul&gt;';
    echo '&lt;li&gt;&lt;h1&gt;PAGES:&lt;/h1&gt;&lt;/li&gt;';
    echo '&lt;li&gt;&lt;ul&gt;';
    wp_list_pages( 'title_li=&amp;echo=1' );
    echo '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;';

    // categories sitemap will be tough...get'em all
    $subcats = wp_list_categories( 'echo=0&amp;use_desc_for_title=0&amp;title_li=' );
    // anybody there?
    preg_match_all( '|&lt;a.*?href=[\'&quot;](.*?)[\'&quot;].*?&gt;|i', $subcats, $m );
	if( !$m[ 1 ] ) {
		// no categs, subcategs or whatsoever...
		echo &quot;There are no categories added to your WordPress site.&quot;;
	} else {
		$arr_categories = array();
		$arr_orig_urls  = array();
		foreach( $m[ 1 ] as $key =&gt; $value ) {
			// $value will keep the URL, extract category slug
			$arr_of_url = explode( '/', $value );
			array_pop( $arr_of_url ); // remove empty
			// push the last
			array_push( $arr_categories, $arr_of_url[ count( $arr_of_url ) - 1 ] );
			// get its URL
			array_push( $arr_orig_urls, $value );
		}
	}

	if( count( $arr_categories ) &gt; 0 ) {
		// now we have entire logical structure of all categories
		// let's try to get ID for each of these
		echo '&lt;ul&gt;';
		echo '&lt;li&gt;&lt;h1&gt;CATEGORIES:&lt;/h1&gt;&lt;/li&gt;';
		$i = 0;
		while( $i &lt; count( $arr_categories ) ) {
			// shortcut by WP built-in
			$idObj = get_category_by_slug( $arr_categories[ $i ] );
			$catid = $idObj-&gt;term_id;

			// check whether this is root category. If true, make an outset in list, otherwise leave inset.
			if( $idObj-&gt;category_parent &gt; 0 ) echo '&lt;li&gt;&lt;ul&gt;';

			// get category real name and link
			$cat_real_name = $wpdb-&gt;get_var( &quot;SELECT name FROM $wpdb-&gt;terms WHERE term_id=&quot; . $catid );
			echo '&lt;li&gt;&lt;h2&gt;&lt;a href=&quot;' . $arr_orig_urls[ $i ] . '&quot;&gt;' . $cat_real_name . '&lt;/a&gt;&lt;/h2&gt;&lt;/li&gt;';

			// generate list of posts
			echo '&lt;li&gt;&lt;ul&gt;';
			query_posts( 'cat=' . $catid  . '&amp;order=ASC' ); // change to DESC - if required
			while( have_posts() ) : the_post();
?&gt;

&lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php
			endwhile;
			echo '&lt;/ul&gt;&lt;/li&gt;';
			if( $idObj-&gt;category_parent &gt; 0 ) echo '&lt;/ul&gt;&lt;/li&gt;';
			$i ++;
		}
	echo '&lt;/ul&gt;';
	}
?&gt;
</code>
</pre>
<p>Hopefully you will find this code useful. Any comment/suggestion regarding improvement is welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://rider.sofarider.com/blog/wordpress-tips/wordpress-simple-site-map-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
