2

I'm trying to setup a sitemap using the craft-sitemap plugin, however it only covers sections by default and I need to add category groups into it, as half of the URLs are in categories.

I can see the Service Methods section in the docs that says that "There’s several service methods made available to add items to the sitemap."

Hooks renderSitemap

Add a renderSitemap method to your plugin to add items via the various service methods listed below.

Here’s an example plugin hook method with comments:

addCategoryGroup(CategoryGroupModel $categoryGroup, [$changefreq, [$priority]])

Adds all categories in the group to the sitemap.

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

But I'm not sure where to start. Where does the hook code go? As I'm assuming I'd have to create a new file, as it will need to be separate from the sitemap plugin as that will be overwritten if there are any updates.

I would appreciate some advice on this please?

  • Just an FYI that the PutYourLightsOn Sitemap plugin handles category groups, Craft Commerce product types and custom URL's out of the box, although it is a commercial plugin: putyourlightson.net/craft-sitemap – Ben Croker May 27 '17 at 20:41
4
+50

You'll need to create a separate plugin, which adds the categories to the sitemap in response to the renderSitemap hook.

Here's what your main plugin file will look like. This should get you 90% of the way there; the rest is just organising a few files and folders, which is well-covered by the official documentation.

<?php

/**
 * SitemapCategories
 *
 * @package Craft
 */

namespace Craft;

/**
 * The main SitemapCategories plugin file.
 */
class SitemapCategoriesPlugin extends BasePlugin
{
    /**
     * Returns the plugin name.
     *
     * @return string
     */
    public function getName()
    {
        return Craft::t('Sitemap Categories');
    }

    /**
     * Returns the plugin description.
     *
     * @return string
     */
    public function getDescription()
    {
        return Craft::t('Adds categories to the sitemap.');
    }

    /**
     * Returns the plugin’s version number.
     *
     * @return string The plugin’s version number.
     */
    public function getVersion()
    {
        return '0.1.0';
    }

    /**
     * Returns the plugin developer’s name.
     *
     * @return string The plugin developer’s name.
     */
    public function getDeveloper()
    {
        return 'Your name here';
    }

    /**
     * Returns the plugin developer’s URL.
     *
     * @return string The plugin developer’s URL.
     */
    public function getDeveloperUrl()
    {
        return 'https://yourwebsite.com';
    }

    /**
     * Returns a faux schema version, so Craft doesn't attempt to run database
     * updates when the plugin version changes.
     *
     * @return string
     */
    public function getSchemaVersion()
    {
        return '0.0.0';
    }

    /**
     * Returns a boolean indicating whether the plugin has settings.
     *
     * @return bool
     */
    public function hasSettings()
    {
        return false;
    }

    /**
     * Returns a boolean indicating whether the plugin has it's own control
     * panel section.
     *
     * @return bool
     */
    public function hasCpSection()
    {
        return false;
    }

    /**
     * Adds all categories to the sitemap.
     */
    public function renderSitemap()
    {
        $groups = craft()->categories->getAllGroups();

        foreach ($groups as $group) {
            craft()->sitemap->addCategoryGroup($group);
        }
    }
}
  • 2
    Whatever it takes to ensure Brad doesn't get that bounty. – Stephen Lewis May 26 '17 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.