Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Created July 10, 2017 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DZuz14/f9201c22eb2673ff9467f8ff1f579a91 to your computer and use it in GitHub Desktop.
Save DZuz14/f9201c22eb2673ff9467f8ff1f579a91 to your computer and use it in GitHub Desktop.
XML Sitemap Generator
<?php
/**
* Generate the sitemap_index file
*/
function generate_main() {
$main_xml = '<?xml version="1.0" encoding="UTF-8"?>';
$main_xml .= "\n";
$main_xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
$main_xml .= "\n\n";
// Creates two sitemap links in sitemap_index. One for posts, and one for pages.
$count = 0;
while($count < 2) {
$main_xml .= "<sitemap>\n";
if($count == 0)
$main_xml .= "\t<loc>\n\t\t" . get_home_url() . "/post-sitemap.xml\n";
else
$main_xml .= "\t<loc>\n\t\t" . get_home_url() . "/page-sitemap.xml\n";
$main_xml .= "\t</loc>\n</sitemap>\n\n";
$count++;
}
// Save the pages to a file
$file = get_home_path() . '/sitemap_index.xml';
$open = fopen($file, "a");
ftruncate($open, 0);
$main_xml .= "</sitemapindex>";
fputs($open, $main_xml);
fclose($open);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment