Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Create a Sitemap

Simple Sitemap

1. Create a snippet called sitemap with the filter set to none and with this code in the body, then save:

<?php
function snippet_sitemap($parent)
{
    $out = '';
    $childs = $parent->children();
    if (count($childs) > 0)
    {
        $out = '<ul>';
        foreach ($childs as $child)
            $out .= '<li>'.$child->link().snippet_sitemap($child).'</li>';
        $out.= '</ul>';
    }
    return $out;
}
?>
<div id="sitemap">
<?php echo snippet_sitemap($this->find('/')); ?>
</div>

2. Create a page called “Sitemap” by clicking on the green “plus” icon beside the Homepage. Copy the following code into the body of the page:

<?php $this->includeSnippet('sitemap'); ?>

Set the filter to none, use your Normal template, and set the page Status to “hidden”, and save.

If you do not set your Sitemap page to “hidden”, it will also appear in the sitemap itself.

3. You can reference the Sitemap link with this code:

<a href="<?php echo BASE_URL; ?>sitemap">Sitemap</a>

which can be included in a footer, sidebar, or wherever.

4. If you want to hide pages that require login (either directly or via inheritance) replace the following code:

foreach ($childs as $child)
    $out .= '<li>'.$child->link().snippet_sitemap($child).'</li>';

with this:

foreach ($childs as $child)
{
    if($child->getLoginNeeded() == Page::LOGIN_REQUIRED)
    {
        continue;
    }
    $out .= '<li>'.$child->link().snippet_sitemap($child).'</li>';
}

snsmurf 2010-01-20 22:12

XML Sitemap (Google friendly)

1. Create a layout called “Xml” with content-type: application/xml, and the body containing only:

<?php echo $this->content(); ?>

2. Create a snippet called xml_sitemap with the filter set to none. Copy this code into the body, then save:

<?php
function snippet_xml_sitemap($parent)
{
    $out = '';
    $childs = $parent->children();
    if (count($childs) > 0)
    {
        foreach ($childs as $child)
        {
            $out .= "  <url>\n";
            $out .= "   <loc>".$child->url()."</loc>\n";
            $out .= "   <lastmod>".$child->date('%Y-%m-%d', 'updated')."</lastmod>\n";
            $out .= "   <changefreq>".($child->hasContent('changefreq') ? $child->content('changefreq'): 'weekly')."</changefreq>\n";
            $out .= "  </url>\n";
            $out .= snippet_xml_sitemap($child);
        }
    }
    return $out;
}
?>
<?php echo '<?'; ?>xml version="1.0" encoding="UTF-8" <?php echo '?>'; ?> 
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php echo snippet_xml_sitemap($this->find('/')); ?>
</urlset>

3. Create a Page called “Sitemap”, set the status to “hidden”, then in body, write:

<?php $this->includeSnippet('xml_sitemap'); ?>

Set the filter to “none”, then set the layout to “Xml”. Save – done!

Excluding Pages Requiring Login

To prevent pages requiring login, either directly or via inheritance, from showing on the sitemap, replace the following code:

foreach ($childs as $child)
{
    $out .= "  <url>\n";
    $out .= "   <loc>".$child->url()."</loc>\n";
    $out .= "   <lastmod>".$child->date('%Y-%m-%d', 'updated')."</lastmod>\n";
    $out .= "   <changefreq>".($child->hasContent('changefreq') ? $child->content('changefreq'): 'weekly')."</changefreq>\n";
    $out .= "  </url>\n";
    $out .= snippet_xml_sitemap($child);
}

With:

foreach ($childs as $child)
{
    if($child->getLoginNeeded() == Page::LOGIN_REQUIRED)
    {
        continue;
    }
    $out .= "  <url>\n";
    $out .= "   <loc>".$child->url()."</loc>\n";
    $out .= "   <lastmod>".$child->date('%Y-%m-%d', 'updated')."</lastmod>\n";
    $out .= "   <changefreq>".($child->hasContent('changefreq') ? $child->content('changefreq'): 'weekly')."</changefreq>\n";
    $out .= "  </url>\n";
    $out .= snippet_xml_sitemap($child);
}

snsmurf 2010-01-20 22:17

Including Published Pages of Hidden Parent Pages

If you wish to “hide” certain pages from your main navigation, but “publish” their child pages, this code will allow these child-of-hidden-page items to be included in the XML sitemap. (This code incorporates the check for protected pages, described above.)

<?php
function snippet_xml_sitemap($parent) {
    $out = '';
    $children = $parent->children(null,array(),true);
    foreach ($children as $child) {
        if ($child->status_id != Page::STATUS_HIDDEN && $child->getLoginNeeded() != Page::LOGIN_REQUIRED) {
            $out .= "  <url>\n";
            $out .= "   <loc>".$child->url()."</loc>\n";
            $out .= "   <lastmod>".$child->date('%Y-%m-%d', 'updated')."</lastmod>\n";
            $out .= "   <changefreq>".($child->hasContent('changefreq') ? $child->content('changefreq'): 'weekly')."</changefreq>\n";
            $out .= "  </url>\n";
        }
        $out .= snippet_xml_sitemap($child);
    }
    return $out;
}
?>
<?php echo '<?'; ?>xml version="1.0" encoding="UTF-8" <?php echo '?>'; ?> 
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php echo snippet_xml_sitemap($this->find('/')); ?>
</urlset>
 
howto/create_a_sitemap.txt · Last modified: 2011-09-12 00:46 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside