A sitemap can make it convenient for website administrators to tell search engines which web page links on their websites are available for crawling. The simplest form of a sitemap is the sitemap.xml file, which lists the URLs on the website along with other metadata about each URL (such as the time it was last updated, how frequently it changes, and its relative importance compared with other URLs on the website), so that search engines can crawl the website more intelligently.
For generating a sitemap.xml sitemap in WordPress, dedicated plugins have already been developed domestically, such as the relatively well-known Baidu Sitemap Generator by Liucheng, while internationally there is Google XML Sitemaps.
However, I feel that using a plugin just to generate a sitemap.xml file is somewhat wasteful of resources, so let's implement it with code! The code was found online and, after making some modifications, I added support for generating XML for tags and categories, as well as the generation time for sitemap.xml.
PHP Code
Create a sitemap.php file and write the following code to it:
<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
?>
<!-- 生成时间 <?php echo $showtime = date("Y-m-d H:i:s"); ?> -->
<url>
<loc><?php echo get_home_url(); ?></loc>
<lastmod><?php $ltime = get_lastpostmodified(GMT); $ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
// 文章
$myposts = get_posts("numberposts=" . $posts_to_show);
foreach ($myposts as $post) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c'); ?></lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php } ?>
<?php
// 页面
$mypages = get_pages();
if (count($mypages) > 0) {
foreach ($mypages as $page) { ?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<lastmod><?php echo str_replace(" ", "T", get_page($page->ID)->post_modified); ?>+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<?php }} ?>
<?php
// 分类
$terms = get_terms('category', 'orderby=name&hide_empty=0');
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) { ?>
<url>
<loc><?php echo get_term_link($term, $term->slug); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.4</priority>
</url>
<?php }} ?>
<?php
// 标签
$tags = get_terms("post_tag");
foreach ($tags as $key => $tag) {
$link = get_term_link(intval($tag->term_id), "post_tag");
if (is_wp_error($link)) return false;
$tags[$key]->link = $link;
?>
<url>
<loc><?php echo $link; ?></loc>
<changefreq>weekly</changefreq>
<priority>0.2</priority>
</url>
<?php } ?>
</urlset>
Upload sitemap.php to the website's root directory, then visit it to view the result.
Pseudo-Static Scheme
Nginx
Edit the existing Nginx pseudo-static rules, add the following rule, and then restart Nginx:
rewrite ^/sitemap.xml$ /sitemap.php last;
Apache
Edit the .htaccess file in the website's root directory and add the following rule:
RewriteRule ^(sitemap)\.xml$ $1.php
Purely Static Scheme
Linux cron job + wget to periodically generate sitemap.xml (this scheme is suitable for VPS users and not applicable to virtual hosts)
Rename sitemap.php in the website's root directory to a PHP file whose name is known only to you, such as xml.php.
Then use a Linux cron job to execute the following command, save the data as sitemap.xml, and place it in the website's root directory:
wget -O /home/www/www.ikxin.com/sitemap.xml https://www.ikxin.com/sitemap.php
https://www.ikxin.com/sitemap.phpis the path to the PHP file./home/www/www.ikxin.com/sitemap.xmlis the path where the XML file is stored.
This solves the problem of sitemap.xml being dynamic data.
