PHP code example of groe / craft-sitemap

1. Go to this page and download the library: Download groe/craft-sitemap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

groe / craft-sitemap example snippets


public function renderSitemap()
{
    // Get an ElementCriteriaModel from the ElementsService
    $criteria = craft()->elements->getCriteria(ElementType::Entry);

    // Specify that we want entries within the ‘locations’ section
    $criteria->section = 'locations';

    // Loop through any entries that were found
    foreach ($criteria->find() as $locationEntry)
    {
        // Here we’re building a path using the entry slug.
        // This might match a custom route you’ve defined that
        // should be ChangeFrequency::Daily, 0.5);
    }
}

$loc = UrlHelper::getSiteUrl('special/route');
$lastmod = new DateTime('now');
craft()->sitemap->addUrl($loc, $lastmod, Sitemap_ChangeFrequency::Yearly, 0.1);

$element = craft()->elements->getElementById(2);
craft()->sitemap->addElement($element, Sitemap_ChangeFrequency::Daily, 1.0);

$section = craft()->sections->getSectionByHandle('homepage');
craft()->sitemap->addSection($section, Sitemap_ChangeFrequency::Weekly, 1.0);

$group = craft()->categories->getGroupByHandle('news');
craft()->sitemap->addCategoryGroup($group);

echo $element->url;
// http://example.com/en/hello-world

echo craft()->sitemap->getElementUrlForLocale($element, 'fr');
// http://example.com/fr/bonjour-monde

echo UrlHelper::getSiteUrl('foo/bar');
// http://example.com/en/foo/bar

echo craft()->sitemap->getUrlForLocale('foo/bar', 'fr');
// http://example.com/fr/foo/bar

Sitemap_ChangeFrequency::Always
Sitemap_ChangeFrequency::Hourly
Sitemap_ChangeFrequency::Daily
Sitemap_ChangeFrequency::Weekly
Sitemap_ChangeFrequency::Monthly
Sitemap_ChangeFrequency::Yearly
Sitemap_ChangeFrequency::Never