WordPress Sitemap Walker

Walker_Sitemap is a basic Page Walker that outputs all pages of your website as simple HTML. This walker is intended to be used to generate a dynamic real time Site Map of your website as it will output the page’s description if available. The walker is compatible with most SEO plugins as it uses the description custom field. One of my favorite uses of the Walker_Sitemap is to include it in my 404 pages so users can quickly and easily find the page they were looking for. To see a quick example follow this link to a non existent page on this website.

Installation and usage

  1. Copy the following code into your templates Theme Functions (functions.php) file.
    /**
     * Create HTML list of pages.
     *
     * @package WordPress
     * @since 2.1.0
     * @uses Walker
     */
    class Walker_Sitemap extends Walker_Page {
    
    	/**
    	 * @see Walker::start_el()
    	 * @since 2.1.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param object $page Page data object.
    	 * @param int $depth Depth of page. Used for padding.
    	 * @param int $current_page Page ID.
    	 * @param array $args
    	 */
    	function start_el(&$output, $page, $depth, $args, $current_page) {
    		if ( $depth )
    			$indent = str_repeat("t", $depth);
    		else
    			$indent = '';
    
    		extract($args, EXTR_SKIP);
    
    		$output .= $indent . '
  2. '; $description = get_post_meta($page->ID, 'description', true); if( ! empty( $description ) ) { $output .= '
    ' . $description . '
    '; } } }
  3. Instantiate the Walker:
    $sWalker = new Walker_Sitemap();
    
  4. Output the Sitemap using the wp_list_pages function of WordPress:
    wp_list_pages( array( 'title_li' => null, 'walker' => $sWalker ) );
    

Comments & Questions

Add Your Comment