RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Generate a Hierarchical Sitemap of WordPress Pages with Shortcode

Generate a Hierarchical Sitemap of WordPress Pages with Shortcode

WordPress pages, unlike posts, are arranged in a child-parent hierarchical relationship. This relationship makes grouping content under a single umbrella a key feature of WordPress. Not totally unlike the shortcode we've previously provided that would render child pages of a parent page, this function, utilizing WordPress' wp_list_pages() function, will display a hierarchical sitemap of all pages on your website, or those that share a parent page.

We considered and quickly moved on from this function when considering how to render our platform help pages. That said, it may still come in useful.

The Result

Printing out all the pages on our site is achieved with the shortcode of [bmsitemap].

■ ■ ■

  1. About Us
  2. Contact
  3. Privacy Policy
  4. Terms of Use
  5. General Disclaimer
  6. BeliefMedia Home Page
  7. Code Reference
  8. Tools
  9. Martin Khoury
  10. Make an Appointment
  11. BeliefMedia Marketing Blog
  12. What We Know About You
  13. Unsubscribe
  14. Thank You For Downloading our Marketing Guide
  15. An Introduction to Search Engine Optimisation
  16. What a Real Marketing Funnel Looks Like
  17. The Funnel Subscription Conditional Redirects
  18. Your Website is a Vital Marketing Asset
  19. Australian RBA Graph and Inflation

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Generate a Hierarchical Sitemap of WordPress Pages with Shortcode
4
 http://www.beliefmedia.com/wordpress-readable-sitemap
5
*/
6
 
7
8
 
9
 $atts = shortcode_atts(array(
10
   'id' => 'sitemap',
11
   'title' => false,
12
   'parent' => false, /* Child of */
13
   'authors' => false,
14
   'depth' => false, /* 1 (any depth), 0 (all pages), 1 (top level only), or depth number */
15
   'sort_solumn' => 'menu_order',
16
   'date_format' => 'j D Y', /* or get_option( 'date_format' ) */
17
   'show_date' => false,
18
   'exclude' => false,
19
   'link_before' => false,
20
   'link_after' => false,
21
   'poststatus' => false,
22
   'item_spacing' => false,
23
   'walker' => false,
24
   'list_style' => 'none',
25
 ), $atts);
26
 
27
  /* If no parent page, defaults to 0 (all) */
28
  $parent = ($atts['parent'] !== false) ? $atts['parent'] : '0';
29
 
30
  /* If no parent page, defaults to 0 (all) */
31
  $authors = ($atts['authors'] !== false) ? $atts['authors'] : '';
32
 
33
  /* Title list */
34
  $title = ($atts['title'] !== false) ? $atts['title'] : '';
35
 
36
  /* Depth */
37
  $depth = ($atts['depth'] !== false) ? $atts['depth'] : '0';
38
 
39
  /* Walker */
40
  $walker = ($atts['walker'] !== false) ? $atts['walker'] : '';
41
 
42
  /* Show date */
43
  $date = ($atts['show_date'] !== false) ? $atts['show_date'] : '';
44
 
45
  /* Exclude pages */
46
  $exclude = ($atts['exclude'] !== false) ? $atts['exclude'] : '';
47
 
48
  /* Post Status */
49
  $poststatus = ($atts['poststatus'] !== false) ? $atts['poststatus'] : 'publish';
50
 
51
  /* Item Spacing */
52
  $spacing = ($atts['item_spacing'] === false) ? 'preserve' : 'discard';
53
 
54
  /* Link after */
55
  $link_after = ($atts['link_after'] !== false) ? $atts['link_after'] : '';
56
 
57
  /* Link before */
58
  $link_before = ($atts['link_before'] !== false) ? $atts['link_before'] : '';
59
 
60
  /* Get map */
61
  $sitemap = wp_list_pages('child_of=' . $parent . '&authors=' . $authors . '&title_li=' . $title . '&depth=' . $depth . '&sort_column=' . $atts['sort_solumn'] . '&walker=' . $walker . '&date=' . $date . '&exclude=' . $exclude . '&post_status=' . $poststatus . '&item_spacing=' . $spacing . '&link_after=' . $link_after . '&link_before=' . $link_before . '&echo=0');
62
 
63
  /* List (change to unordered with ul tags */
64
  if ($sitemap != '') $sitemap = '<ol' . ($atts['id'] == '' ? '' : ' id="' . $atts['id'] . '"') . '>' . $sitemap . '</ol>
65
 
66
';
67
 
68
 return '' . $sitemap . '';
69
}
70
add_shortcode('bmsitemap', 'beliefmedia_wordpress_readable_sitemap');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

An alternate method of printing the map (and that which is documented) would be passing the arguments in an array (the function uses wp_parse_args() to merge the $args with the $defaults values). In fact, we could do away with the virtually every statement after the $atts array, and pass the $atts array directly to the wp_list_pages() function.

Most of the shortcode attributes are detailed on the WordPress Codex page . Those that aren't are parent (the parent page of the list) and list_style. The list styles are detailed on W3schools (and WP ).

Considerations

  • If you want to create a custom list, consider create a custom walker class to extend wp_list_pages() (inheriting the properties of the Walker_Page class). Usage of an extended class essentially means you can modify the list in virtually any way.

Download


Title: Sitemap of WordPress Pages (WP Plugin)
Description: Generate a Hierarchical Sitemap of WordPress Pages with Shortcode.
  Download • Version 0.2, 1.9K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.1K)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment