Created
November 28, 2014 17:38
-
-
Save biojazzard/048a4b16b899580fe521 to your computer and use it in GitHub Desktop.
ProcessWIre SiteMap Generator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Based on Ryan Cramer´s Code | |
* ProcessWire Template to power a sitemap.xml | |
* | |
* Purpose: Render sitemap.xml for this special conditions | |
* | |
* / [home](/) Without children! | |
* /page-that-holds-the-tree/ [Page that holds the tree of the web.] With all his children. | |
* | |
* Add more pages in the Array(). | |
* | |
* 1. Copy this file to /site/templates/sitemap-xml.php | |
* 2. Add the new template from the admin. | |
* Under the "URLs" section, set it to NOT use trailing slashes. | |
* 3. Create a new page at the root level, use your sitemap-xml template | |
* and name the page "sitemap.xml". | |
* | |
* Note: hidden pages (and their children) are excluded from the sitemap. | |
* If you have hidden pages that you want to be included, you can do so | |
* by specifying the ID or path to them in an array sent to the | |
* renderSiteMapXML() method at the bottom of this file. For instance: | |
* | |
* echo renderSiteMapXML(array('/hidden/page/', '/another/hidden/page/')); | |
* | |
*/ | |
$useMain = false; | |
function renderSitemapPage(Page $page) { | |
return | |
"\n<url>" . | |
"\n\t<loc>" . $page->httpUrl . "</loc>" . | |
"\n\t<lastmod>" . date("Y-m-d", $page->modified) . "</lastmod>" . | |
"\n</url>"; | |
} | |
function renderSitemapChildren(Page $page) { | |
$out = ''; | |
$newParents = new PageArray(); | |
$children = $page->children; | |
foreach($children as $child) { | |
$out .= renderSitemapPage($child); | |
if($child->numChildren) $newParents->add($child); | |
else wire('pages')->uncache($child); | |
} | |
foreach($newParents as $newParent) { | |
$out .= renderSitemapChildren($newParent); | |
wire('pages')->uncache($newParent); | |
} | |
return $out; | |
} | |
function renderSitemapXML(array $paths = array()) { | |
$out = | |
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" . | |
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; | |
//array_unshift($paths, '/'); // prepend homepage | |
$out .= renderSitemapPage(wire('pages')->get('/')); | |
foreach($paths as $path) { | |
$page = wire('pages')->get($path); | |
if(!$page->id) continue; | |
$out .= renderSitemapPage($page); | |
if($page->numChildren) $out .= renderSitemapChildren($page); | |
} | |
$out .= "\n</urlset>"; | |
return $out; | |
} | |
header("Content-Type: text/xml"); | |
echo renderSitemapXML(array('/page-that-holds-the-tree/')); | |
// If you want to include other hidden pages: | |
// echo renderSitemapXML(array('/path/to/hidden/page/')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment