-
Notifications
You must be signed in to change notification settings - Fork 4
/
xmlSitemapMultilang.php
101 lines (69 loc) · 2.34 KB
/
xmlSitemapMultilang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
create an XML sitemap for every language
why? Google et al usually only accepts one single XML, not one for each language
With PW multilang. setup, you'd have normally to manually "glue" these XMLs together
e.g. site.com/en/sitemap.xml + site.com/de/sitemap.xml + site.com/fr/sitemap.xml etc.
Functions taken from Ryan Cramer:
http://processwire.com/talk/topic/3846-how-do-i-create-a-sitemapxml/?p=37613
*/
// opening XML tag + node:
$sitemapCollection = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
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()) {
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);
}
return $out;
}
// DE: (default lang. here)
$user->language = $languages->get("default");
$sitemapCollection .= renderSitemapXML();
$pgs = $pages->find("template=product, include=hidden, sort=sort");
foreach ($pgs as $p) {
$sitemapCollection .= "\n<url>" .
"\n\t<loc>" . $p->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $p->modified) . "</lastmod>" .
"\n</url>";
}
// EN:
$user->language = $languages->get("en");
$sitemapCollection .= renderSitemapXML();
$pgs = $pages->find("template=product, include=hidden, sort=sort");
foreach ($pgs as $p) {
$sitemapCollection .= "\n<url>" .
"\n\t<loc>" . $p->httpUrl . "</loc>" .
"\n\t<lastmod>" . date("Y-m-d", $p->modified) . "</lastmod>" .
"\n</url>";
}
// etc. - repeat for each language
// close XML node
$sitemapCollection .= "\n</urlset>";
header("Content-Type: text/xml");
echo $sitemapCollection;
?>