【问题标题】:Customized Sitemap Generate自定义站点地图生成
【发布时间】:2021-07-12 04:59:24
【问题描述】:

我使用这个库创建了站点地图https://github.com/spatie/laravel-sitemap 但我不想 changefreq info.how to disable.can't find in repository

SitemapGenerator::create(url('/'))->writeToFile(public_path('sitemap.xml'));

结果

<url>
    <loc>http://the-frenemy.local</loc>
    <lastmod>2021-04-16T00:00:00+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
</url>

另外一个变更集优先级是 1 只有主页..所有其他页面设置为 0.9

【问题讨论】:

    标签: php laravel web-crawler sitemap laravel-sitemap


    【解决方案1】:

    发布资源视图

    然后编辑文件:https://github1s.com/spatie/laravel-sitemap/blob/HEAD/resources/views/url.blade.php

    <url>
        @if (! empty($tag->url))
        <loc>{{ url($tag->url) }}</loc>
        @endif
    @if (count($tag->alternates))
    @foreach ($tag->alternates as $alternate)
        <xhtml:link rel="alternate" hreflang="{{ $alternate->locale }}" href="{{ url($alternate->url) }}" />
        @endforeach
    @endif
    @if (! empty($tag->lastModificationDate))
        <lastmod>{{ $tag->lastModificationDate->format(DateTime::ATOM) }}</lastmod>
    @endif
    
        // remove this
        @if (! empty($tag->changeFrequency))
        <changefreq>{{ $tag->changeFrequency }}</changefreq>
        @endif
        // remove this
    
    @if (! empty($tag->priority))
        <priority>{{ number_format($tag->priority,1) }}</priority>
        @endif
    </url>
    
    

    【讨论】:

    • thanx Chung 知道如何为不同的 URL 设置优先级吗?
    • SitemapGenerator::create('https://example.com')-&gt;getSitemap()-&gt;add(Url::create('/extra-page')-&gt;setPriority(0.1))-&gt;writeToFile($path); 使用setPriority方法支持-&gt;setPriority(0.1))
    • SitemapGenerator::create(url('/'))->getSitemap()->add(Url::create(url('/'))->setPriority(0.9))-> writeToFile(public_path('sitemap.xml'));此语句不改变基本 url 的优先级
    • @FrederickaHartman 编辑起来很复杂,如果你懂php,我会解释如下: $sitemap = SitemapGenerator::create('example.com')->getSitemap(); $sitemap is instanceof Spatie\Sitemap\站点地图。要编辑优先级,我们必须编辑数组 $sitemap->tags 中的每个项目。但是 $tags 是受保护的属性,setTags 方法不支持只有 getTags 方法。
    • 那么我们该怎么做.. $sitemapTags = $sitemap->getTags(); $newSitemap = 站点地图::create(); foreach ($sitemapTags as $tag) { $tag-&gt;setPriority(0.5); $newSitemap->add($tag); } $newSitemap-&gt;writeToFile($path);
    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 2015-08-11
    • 2023-01-21
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    相关资源
    最近更新 更多