Skip to content

Instantly share code, notes, and snippets.

@developer-anuragsingh
Last active October 28, 2022 07:25
Show Gist options
  • Save developer-anuragsingh/e3f42adcb347d57b435978956991056b to your computer and use it in GitHub Desktop.
Save developer-anuragsingh/e3f42adcb347d57b435978956991056b to your computer and use it in GitHub Desktop.
Add WordPress's html sitemap with Shortcode. Also exclude pages which having 'sitemap' word in the title.
<?php
/**
* Create sitemap with shortcode
*/
function ans_sitemap($atts)
{
// Get all pages
$pages = get_pages();
foreach ($pages as $page) {
// Convert title in lowercase and make array of each word and check it 'sitemap' exits in the array
if (in_array('sitemap', explode(' ', strtolower($page->post_title)))) {
// Prepare array of all page IDs, which have 'sitemap' word in the page title.
$pages_to_exclude[] = $page->ID;
}
}
echo '<h2 class="sitemap">Sitemap</h2>';
echo '<ul>';
wp_list_pages(array(
'title_li' => false,
'sort_column' => 'menu_order',
'exclude' => implode(',', $pages_to_exclude)
));
echo '</ul>';
}
add_shortcode('sitemap', 'ans_sitemap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment