1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php 
 
 /*
 Template Name: Sitemap Page
 */
 
get_header();
 
	/* PAGES
	----------------------------------------------*/
 
	// Get the navigation items to display the same order and save them in array to exclude later
 
	$items = wp_get_nav_menu_items( 'my menu id' );
	$exclude_pages = get_the_ID(); // to not display current sitemap page
 
	echo '<ul>';
 
 	foreach( $items as $item )  {
 
                $class = '';
		$exclude_pages .= ',' . $item->object_id;
 
		if( $item->menu_item_parent != 0) {
			$class = ' class="sitemap-item-child"';
		}
 
		?>
			<li<?php echo $class ?>>
				<a href=""><?php echo $item->title; ?></a>
			</li>
 
		<?php
 
	}
 
	// List the rest of the pages that are not in the navigation
	wp_list_pages(
			 array(
			   'exclude' => $exclude_pages,
			   'title_li' => '',
			 )
		);
 
	echo '</ul>';
 
	/* CUSTOM POST TYPES 
	----------------------------------------------*/
 
	foreach( get_post_types( array('public' => true) ) as $post_type ) {
 
	  if ( in_array( $post_type, array('post','page','attachment') ) ) continue;
 
	  $item = get_post_type_object( $post_type );
 
	  echo '<h2>'.$item->labels->name.'</h2>';
 
	  $args = array(
		'post_type' => $post_type,
		'posts_per_page'    => -1
	  );
 
	  $query = new WP_Query( $args );
 
	  echo '<ul>';
 
		while ( $query->have_posts() ) {
 
			$query->the_post();
			echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
 
		}
 
	  echo '</ul>';
 
	}
 
 
get_footer();
Create Sitemap HTML Page

Leave a Reply

Your email address will not be published. Required fields are marked *