-
Notifications
You must be signed in to change notification settings - Fork 63
Add alternate links to sitemap #6
Comments
+1 |
1 similar comment
+1 |
I solved changing line
with
Your first argument for addItem should be an array with keys 'default' or with the hreflang code and urls as values
|
ok, i think its time for a PR or a simple fork here. ☀️ |
Sorry, i left something. The next line should be added to privat method startSitemap
|
I will do the PR today. |
…sue evert#6 In order to have alternate links to sitemap the first argument of addItem method should be an array where keys are 'default' or hreflang code and values the different url's.
PR done right now ! |
I just change addItem() in this way: /**
* Adds an item to sitemap
*
* @param string $loc URL of the page. This value must be less than 2,048 characters.
* @param string|null $priority The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
* @param string|null $changefreq How frequently the page is likely to change. Valid values are always, hourly, daily, weekly, monthly, yearly and never.
* @param string|int|null $lastmod The date of last modification of url. Unix timestamp or any English textual datetime description.
* @return Sitemap
*/
public function addItem($loc, $priority = self::DEFAULT_PRIORITY, $changefreq = NULL, $lastmod = NULL, $alternateLanguage = null) {
if (($this->getCurrentItem() % self::ITEM_PER_SITEMAP) == 0) {
if ($this->getWriter() instanceof \XMLWriter) {
$this->endSitemap();
}
$this->startSitemap();
$this->incCurrentSitemap();
}
$this->incCurrentItem();
$this->getWriter()->startElement('url');
$this->getWriter()->writeElement('loc', $this->getDomain() . $loc);
if($priority !== null)
$this->getWriter()->writeElement('priority', $priority);
if ($changefreq)
$this->getWriter()->writeElement('changefreq', $changefreq);
if ($lastmod)
$this->getWriter()->writeElement('lastmod', $this->getLastModifiedDate($lastmod));
// Check if exist alternate language
if ($alternateLanguage) {
$this->getWriter()->startElement('xhtml:link');
$this->getWriter()->writeAttribute(
'rel',
'alternate'
);
$this->getWriter()->writeAttribute(
'hreflang',
$alternateLanguage['hreflang']
);
$this->getWriter()->writeAttribute(
'href',
$this->getDomain() . $alternateLanguage['href']
);
$this->getWriter()->endElement();
}
$this->getWriter()->endElement();
return $this;
} Usage: include 'Sitemap.php';
$sitemap = new \SitemapPHP\Sitemap(http://example.com);
$sitemap->setPath('xmls/');
$sitemap->addItem('/', '1.0', 'daily', 'Today', ['hreflang' => 'de', 'href' => '/de']);
$sitemap->addItem('/about', '0.8', 'monthly', 'Jun 25', ['hreflang' => 'de', 'href' => '/de/about']);
$sitemap->addItem('/contact', '0.6', 'yearly', '14-12-2009', ['hreflang' => 'de', 'href' => '/de/contact']);
$sitemap->addItem('/otherpage', null, null, null, ['hreflang' => 'de', 'href' => '/de/otherpage']);
$sitemap->createSitemapIndex('http://example.com/xmls/', 'Today'); Plus in startSitemap() $this->getWriter()->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); Output: <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://example.com/</loc>
<priority>1.0</priority>
<changefreq>daily</changefreq>
<lastmod>2017-03-22</lastmod>
<xhtml:link rel="alternate" hreflang="de" href="/de"/>
</url>
<url>
<loc>http://example.com/about</loc>
<priority>0.8</priority>
<changefreq>monthly</changefreq>
<lastmod>2017-06-25</lastmod>
<xhtml:link rel="alternate" hreflang="de" href="/de/about"/>
</url>
<url>
<loc>http://example.com/contact</loc>
<priority>0.6</priority>
<changefreq>yearly</changefreq>
<lastmod>2009-12-14</lastmod>
<xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/contact"/>
</url>
<url>
<loc>http://example.com/otherpage</loc>
<xhtml:link rel="alternate" hreflang="de" href="http://example.com/de/otherpage"/>
</url>
</urlset>
The only difference that I notice is that browser display sitemap differently, with blank screen and links as string. But also google sitemap example is the same, this https://support.google.com/webmasters/answer/2620865?hl=en |
Hi,
I think it would be great if there would be a way to set an alternate link in the sitemap for mobile pages. It's quite easy and described here. I'm not sure what the best way to add this would be.
If you do not have a separate mobile Domain the easiest thing to do would be to add more params to the addItem() function for the alternate link and the media string. but there are already 4 params. At least it wouldn't break anything :-).
But if you have a separate mobile domain (like m.) you would have to set an extra mobile Domain or something like that. So in most cases it would be enough to set the mobile Domain and the media string one time and then automatically create an alternate link. What do you think?
The text was updated successfully, but these errors were encountered: