rentageekmom of LDM Devs
1/31/2017 - 2:29 PM

Sitemap Generator

Sitemap Generator

/************* GENERATE HTML SITEMAP *************/

if (isset($_GET['activated']) && is_admin()){
        $new_page_title = 'Sitemap';
        $new_page_content = '';
        $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
        
        //don't change the code below
        $page_check = get_page_by_title($new_page_title);
        $new_page = array(
                'post_type' => 'page',
                'post_title' => $new_page_title,
                'post_content' => $new_page_content,
                'post_status' => 'publish',
                'post_author' => 1,
        );
        if(!isset($page_check->ID)){
                $new_page_id = wp_insert_post($new_page);
                if(!empty($new_page_template)){
                        update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                }
        }
}


function GenerateSitemap($params = array()) {
    // default parameters
    extract(shortcode_atts(array(
        'title' => 'Sitemap',
        'exclude' => 'Sitemap',
        'depth' => 2,
    ), $params));
    // create sitemap
	if ($exclude !== 'Sitemap') {
$sitemap = wp_list_pages('title_li='.$title.'&depth='.$depth.'&sort_column=menu_order&exclude='.$exclude);
	} else {
		$sitemapPage = get_page_by_title($exclude,OBJECT);
		$sitemap = wp_list_pages('title_li='.$title.'&depth='.$depth.'&sort_column=menu_order&exclude='.$sitemapPage->ID);
}
    if ($sitemap != '') {
        $sitemap =
            ($title == '' ? '' : "<h2>$title</h2>") .
            "<ul>$sitemap</ul>";
    }
    return $sitemap;
}
add_shortcode('sitemap', 'GenerateSitemap');