Create a Sitemap

Create a snippet called sitemap with the filter set to none and having this code in the body:

<?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>

Create a page called “sitemap” by clicking on the green “plus” icon beside the Home page; the body of the page should simply be:

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

Use your Normal template.

Note: set your Sitemap page to hidden if you don’t want it to appear in the sitemap itself.

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!

Now to create a XML Sitemap (Google like)

Create a layout Xml with content-type:
application/xml, and body: <?php echo $this->content(); ?>

Create a snippet called xml_sitemap with the filter set to none. Copy this into the body:

<?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>

Create your sitemap Page, 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.


Frog CMS Demo

Public front end website click here
Administration backend click here

Opensource.com admin login info:
login: admin
password: demo123

Help us!

There is no profit here. The more we raise the longer we can keep things going and the better it will get.

Click here to lend your support to: Frog CMS and make a donation at www.pledgie.com !