iBegins
 

Tạo file sitemap không cần dùng Plugin cho WordPress

Chào bạn nhé, hôm nay mình hướng dẫn bạn cách tạo sitemap không cần dùng plugin.

Tớ có đoạn code sau:

<?php 
/*
 * Template Name: Sitemap
 */</pre>
header("Content-type: text/xml");

echo '<?xml version="1.0" encoding="UTF-8" ?>';

?>

<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.sitemaps.org/schemas/sitemap/0.9 https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:example="http://www.example.com/schemas/example_schema">

<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod>2010-04-20</lastmod>
<changefreq>always</changefreq>
<priority>1.00</priority>
</url>

<?php

$postquery = new WP_Query(array('posts_per_page' => 10));
if ($postquery->have_posts()) {
while ($postquery->have_posts()) : $postquery->the_post();

$title = get_permalink();
$date = get_the_date( "Y-m-d");

echo "

<url>
<loc>".$title."</loc>
<lastmod>".$date."</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
</url>";

endwhile;} ?>

</urlset>
<pre>

1, Bạn dùng đoạn code trên tạo cho nó một file trong wp-content/themes/folder theme của bạn/sitemap.php.
2, Khi tạo xong bạn truy cập vào wp-admin tạo cho mình 1 trang (Page) và thêm thuộc tính trang > Giao diện > Sitemap.
3, Tiếp theo bạn vào cập nhật đường dẫn tính dưới dạng Tiêu đề bài viết chọn https://domain.com/bai-mau/ rồi lưu lại.
4, Tiếp đến bạn vào lại Page vừa tạo ở bước 2 nhấn xem trang và copy đường dẫn dạng https://domain.com/sitemap/ vào google console rồi check thôi.
Chúc bạn thành công nhé. Đừng quên Share bài viết nếu thấy hay.

Ngày 4 Tháng Sáu, 2019 ( 649 )

Tagged with: , , ,

Bình luận bài viết

Đăng nhập để bình luận và xem các bình luận khác.