Processwire 3中的Sitemap-XML

时间:2016-10-20 14:57:18

标签: sitemap processwire

如何为huebert-webentwicklung.de/sitemap.xml为processwire 3生成站点地图,它可以使用Plugin MarkupSitemapXML。知道如何让它发挥作用吗?

感谢。

1 个答案:

答案 0 :(得分:1)

创建新的页面模板(sitemap.xml),然后将页面输出设置为PW后端的XML。创建一个页面并将其链接(将其设置为隐藏)。

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

  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();