Skip to content
This repository has been archived by the owner on Dec 24, 2019. It is now read-only.

Add alternate links to sitemap #6

Open
nonsenz opened this issue May 6, 2015 · 8 comments
Open

Add alternate links to sitemap #6

nonsenz opened this issue May 6, 2015 · 8 comments

Comments

@nonsenz
Copy link

nonsenz commented May 6, 2015

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?

@oscarotero
Copy link

+1

1 similar comment
@dfsmirnov
Copy link

+1

@rperello
Copy link

I solved changing line

$this->getWriter()->writeElement('loc', $this->getDomain() . $loc); in addItem method

with

if (is_string($loc)) { $this->getWriter()->writeElement('loc', $this->getDomain() . $loc); } else if (is_array($loc)) { foreach ($loc as $language => $alternate_loc) { if ($language == 'default') { $this->getWriter()->writeElement('loc', $this->getDomain() . $alternate_loc); $this->getWriter()->startElement('xhtml:link'); $this->getWriter()->writeAttribute('rel', 'alternate'); $this->getWriter()->writeAttribute('hreflang', 'x-default'); $this->getWriter()->writeAttribute('href', $this->getDomain() . $alternate_loc); $this->getWriter()->endElement(); } else { $this->getWriter()->startElement('xhtml:link'); $this->getWriter()->writeAttribute('rel', 'alternate'); $this->getWriter()->writeAttribute('hreflang', $language); $this->getWriter()->writeAttribute('href', $this->getDomain() . $alternate_loc); $this->getWriter()->endElement(); } } }

Your first argument for addItem should be an array with keys 'default' or with the hreflang code and urls as values

array( 'default' => $default_url, 'de' => $url_to_german, 'es' => $url_to_spanish, )

@nonsenz
Copy link
Author

nonsenz commented Apr 16, 2016

ok, i think its time for a PR or a simple fork here. ☀️

@rperello
Copy link

Sorry, i left something. The next line should be added to privat method startSitemap

$this->getWriter()->writeAttribute('xmlns:xhtml' ,"http://www.w3.org/1999/xhtml");

@rperello
Copy link

I will do the PR today.

rperello added a commit to rperello/sitemap-php that referenced this issue Apr 16, 2016
…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.
@rperello
Copy link

PR done right now !

@andreakru
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants