generate sitemap in Eleventy (11ty)

#11ty20 ต.ค. 2567

generate sitemap in Eleventy (11ty)

เราไปดูวิธีการทำ sitemap.xml ใน 11ty กันครับ ตามตัวอย่างโค้ดด้านล่างเลย

---
permalink: /sitemap.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>https://www.doesystem.com</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    {% set data_release_reverse = data_release | sortById %}

    {% for item in data_release_reverse %}
    <url>
        <loc>https://www.doesystem.com/board/{{item.id}}</loc>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    {% endfor %}
</urlset>

จากโค้ด เราจะใส่ permalink: /sitemap.xml เพื่อบอกว่าให้ generate เป็น sitemap.xml

จากนั้นเราก็วนลูปข้อมูล เพื่อ genarate sitemap ตามข้อมูลที่มี