Thesis XHTML Sitemap V2

May 5, 2009

Post image for Thesis XHTML Sitemap V2

The initial code for this worked well, but justme on the Support boards (login required) for the Thesis theme wanted something similar, but with each post listed under their respective categories.

Instructions

  1. Create a new page, and save it with the name “sitemap”.
  2. Make sure the Page Template selection is set to “Custom Template”.
  3. Open and prepare to edit your custom_functions.php file.
  4. Add the following:
    function xhtml_sitemap() {
    if (is_page('sitemap') || is_page('707')) { ?>
    
    <div id="content">
    <div class="post_box top">
    <div class="headline_area">
    	<h1><?php the_title(); ?></h1>
    </div>
    <div class="format_text">
      <h3>All pages:</h3>
    <ul>
    	<?php wp_list_pages('title_li='); ?>
    </ul>
    <h3>All blog posts:</h3>
    <ul>
    	<?php echo posts_by_category(); ?>
    </ul>
    <h3>Monthly archive pages:</h3>
    <ul>
    	<?php wp_get_archives('type=monthly'); ?>
    </ul>
    </div>
    </div>
    </div>
    <div id="sidebars">
    <?php thesis_build_sidebars(); ?>
    </div>		
    
    <?php } }
    
    add_action('thesis_hook_custom_template', 'xhtml_sitemap');
    
    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 href="' . get_category_link($posts->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 href="' . get_permalink($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a></li>';
    	  }
        $the_output .= '</ul></li>';
      }
      return $the_output;
    }
    

Tweaks

My sitemap page has the slug “sitemap” and has an ID of 707. Although you might have created your page with the same slug, you should amend the 707 to the ID of your page. You can find this by hovering over the relevant edit link on the Edit Posts page.

Obviously, remember you may also need to add starting and ending PHP delimiters (<?php and ?>) depending on where you put this in your custom_functions.php file.

You may also need to change the div structure to suit how your overall site layout.

Add a nice bit of JavaScript to collapse the sub unordered lists, and it will improve it’s usability no-end. Search engine bots won’t collapse the lists, and could still traverse all links, while humans would not be confronted with a stupid amount of links.

Result

You can see a demo of what your new sitemap page will look like by checking out mine

About the Author

Gary Jones

Gary Jones is a web developer from Basingstoke with a keen interest in anything to do with the web. He runs Gamajo Tech, a freelance web development company in Basingstoke, delivering sites and code professionally to small businesses and individuals.

Get social with Gary: Twitter | Facebook | LinkedIn | Last.fm

If you enjoyed this post, please consider leaving a comment and subscribing to the RSS feed or subscribing via email to have future articles delivered to your feed reader or email inbox.

{ 3 trackbacks }

links for 2009-05-06 — girliegeek
May 6, 2009 at 07:04
Thesis Tutorial Compilation - The Most Useful Thesis Theme Tutorials on the Web organized by subject and author
June 29, 2009 at 15:55
Super Thesis Tips | Anthony Lepki
December 19, 2009 at 07:54

{ 13 comments }

Kate Davis May 5, 2009 at 10:27

Can’t comment on your code, but I did look at your site map. The first entry in your map is for something to do with British Sign Language although its password protected so I don’t know what its about, but thought I’d tell you that Beth and I have started going to Tiny Talk classes which is baby sign language based on BSL.

Reply

Gary May 5, 2009 at 12:31

Yep, probably Makaton? It’s like a simplified version of BSL.

As for the password link, yep, as it contains contact details for members of the group – sorry!

Reply

Roberto Gerola May 6, 2009 at 16:43

Hi Gary.
Many thanks to share this code.
I’m using thesis 1.5, and I had to add the following row of code in my custom_functions.php file, otherwise the title and sidebar were added two times.

remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');

Roberto

Reply

Gary May 7, 2009 at 12:14

Yep, I suspect my template or layout will differ from others. Thanks for sharing your fix though – may help others :)

Reply

Avinash June 13, 2009 at 13:17

Hi Gary,

Need a bit of help on this code…

1)Create a page called sitemap and add to nav menu
2)Add in code to custom_functions.php file changing the page id

I just end up going to a page with a post called sitemap and nothing else…what am I doing wrong?I’m testing locally so I can’t give you a link to see what’s goin on…

Sitemap
by admin[edit]

Reply

Gary June 13, 2009 at 13:21

You’ll need to make sure that the page template is set to “Custom Template” so that Thesis tries to take notice of anything hooked into it via your custom_functions.php.

Reply

Avinash June 13, 2009 at 16:08

That’s done too…sorry I didn’t mention that earlier.

Not sure if this helps but this is also on the Sitemap page…
Main Page(no parent)
Order(0)

Reply

Gary June 13, 2009 at 16:11

Have you changed 707 in the example code, to whatever your postID for your Sitemap page is?
Which Thesis version are you using?

Reply

Avinash June 14, 2009 at 16:29

Done that too….page ID of 27. And I’m on Thesis 1.5.1…do you want a look at my code perhaps?

Reply

Radhika August 22, 2009 at 15:08

Hi Gary,

I am having exactly the same problems as Avinash. I published the page “Sitemap” choosing the custom template and pasted the code you provided in the customs function.php, changing the page ID. Although the sitemap exists if I search for the url (mydomain.com/sitemap), it is nowhere on my site. Can you help me?

Many thanks,
Radhika

Reply

Gary August 23, 2009 at 11:09

Hi Radhika,

It may just be that you need to edit the footer to add in a link to the sitemap, or place it in a text widget in the sidebar, really, wherever you want it. I can see that it’s currently in your Popular Articles list of links on the right hand side of your site.

Reply

Radhika August 23, 2009 at 12:30

Hi Gary,

Many thanks for replying! I have been trying to figure out how to add the sitemap link to the footer since I don’t really want it in my sidebar. Is there a quick and easy way, including necessary code, that you could suggest?

Sorry for being a pest and thanks again!
Radhika

Reply

Gary August 23, 2009 at 12:38

A good place to look for how to customise the footer, is on Answers.

Good luck!

Reply

Comments on this entry are closed.

Previous post:

Next post: