groe/craft-sitemap

Craft插件,用于生成网站地图。

维护者

详细信息

github.com/groe/craft-sitemap

源代码

安装: 1,134

依赖关系: 0

建议者: 0

安全性: 0

星级: 23

关注者: 3

分支: 29

类型:craft-plugin

v1.2.3 2018-04-18 08:16 UTC

This package is not auto-updated.

Last update: 2024-09-26 19:06:45 UTC


README

注意:这是一个目前未维护的由@joshuabaker维护的craft-sitemap插件的分支。原始作者未回应有关该项目未来计划的问题。所以,在我们(希望)某天将其合并回来之前,您可以随意使用这个分支。

Craft Sitemap

一个简单的Craft插件,根据启用的部分生成sitemap.xml

Settings

安装

  1. sitemap/文件夹复制到craft/plugins/
  2. 转到设置→插件,然后点击“Sitemap”旁边的“安装”按钮

使用方法

在插件设置中,勾选“启用”列中的复选框以将其包含在网站地图中。

要查看输出,请访问/sitemap.xml

高级

此插件公开了各种服务方法,可以通过renderSitemap钩子添加自定义项目到网站地图。如果您不确定这是如何工作的,请阅读官方的“钩子和事件”文档

钩子

renderSitemap

将一个renderSitemap方法添加到您的插件中,通过以下列出的各种服务方法添加项目。

以下是一个带有注释的插件钩子方法示例

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 included in the sitemap.
        $path = 'cars-for-sale-in-' . $locationEntry->slug;

        // Make sure that we’re using a full URL, not just the path.
        $url = UrlHelper::getSiteUrl($path);

        // For the sake of this example, we’re setting the $lastmod
        // value to the most recent time the location entry was
        // updated. You can pass any time using the DateTime class.
        $lastmod = $locationEntry->dateUpdated;

        // Add the URL to the sitemap
        craft()->sitemap->addUrl($url, $lastmod, Sitemap_ChangeFrequency::Daily, 0.5);
    }
}

以下是在网站地图XML中的结果元素示例

<url>
  <loc>http://example.com/cars-for-sale-in-scotland</loc>
  <lastmod>2015-08-28T15:08:28+00:00</lastmod>
</url>

服务方法

提供了一些服务方法来添加项目到网站地图。

addUrl($loc, $lastmod, [$changefreq, [$priority]])

向网站地图添加URL。

$loc = UrlHelper::getSiteUrl('special/route');
$lastmod = new DateTime('now');
craft()->sitemap->addUrl($loc, $lastmod, Sitemap_ChangeFrequency::Yearly, 0.1);
addElement(BaseElementModel $element, [$changefreq, [$priority, [$currentLocaleOnly, [$alternateUrls]]]])

向网站地图添加元素。

$element = craft()->elements->getElementById(2);
craft()->sitemap->addElement($element, Sitemap_ChangeFrequency::Daily, 1.0);
addSection(SectionModel $section, [$changefreq, [$priority, [$currentLocaleOnly, [$alternateUrls]]]])

向网站地图添加部分的全部条目。

$section = craft()->sections->getSectionByHandle('homepage');
craft()->sitemap->addSection($section, Sitemap_ChangeFrequency::Weekly, 1.0);
addCategoryGroup(CategoryGroupModel $categoryGroup, [$changefreq, [$priority, [$currentLocaleOnly, [$alternateUrls]]]])

向网站地图添加组中的所有分类。

$group = craft()->categories->getGroupByHandle('news');
craft()->sitemap->addCategoryGroup($group);
getElementUrlForLocale(BaseElementModel $element, $locale)

获取指定地区的元素URL。地区必须已启用。

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

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

获取指定地区的URL。地区必须已启用。

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

有效的changefreq值的枚举。

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