全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 757|回复: 4

[疑问] #DUX#WordPress 更新文章时生成站点地图xml 代码版 (sitemap.xml)

[复制链接]
发表于 2019-5-11 21:04:17 | 显示全部楼层 |阅读模式
如果以下代码有任何错误,请告知,我测试是没问题的。哈,可能复制的时候没复制全等原因,感谢!!!
小站原文:https://cvps.top/9308.html
方法
以下方法适合DUX主题。其他主题自行修改相对位置位置,谢谢。

在主题目录中options.php 添加开关。(至于放哪个位置,看你喜欢!)
  1.         $options[] = array(
  2.                 'name' => __('更新文章时生成站点地图xml', 'haoui'),
  3.                 'desc' => __('开启', 'haoui'),
  4.                 'id' => 'sitemap_xml',
  5.                 'std' => true,
  6.                 'type' => 'checkbox');
复制代码

在主题目录 functions.php 添加:
  1. // sitemap_xml
  2. if (_hui('sitemap_xml')) {  
  3.     function cvps_sitemap_refresh() {
  4.         require_once get_template_directory() . '/modules/sitemap-xml.php';
  5.         $sitemap_xml = cvps_get_xml_sitemap();
  6.         file_put_contents(ABSPATH.'sitemap.xml', $sitemap_xml);
  7.     }
  8.     if ( defined('ABSPATH') ) {
  9.         add_action('publish_post', 'cvps_sitemap_refresh');
  10.         add_action('save_post', 'cvps_sitemap_refresh');
  11.         add_action('edit_post', 'cvps_sitemap_refresh');
  12.         add_action('delete_post', 'cvps_sitemap_refresh');
  13.     }
  14. }
复制代码

在主题目录中 modules 文件夹 新建 sitemap-xml.php 并添加以下代码:
  1. <?php
  2. function cvps_get_xml_sitemap() {
  3.         ob_start();
  4.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  5. ?>
  6. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">
  7. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> -->
  8. <url>
  9. <loc><?php echo get_home_url(); ?></loc>
  10. <lastmod><?php echo gmdate('Y-m-d\TH:i:s+00:00', strtotime(get_lastpostmodified('GMT'))); ?></lastmod>
  11. <changefreq>daily</changefreq>
  12. <priority>1.0</priority>
  13. </url>
  14. <?php
  15.         // 文章
  16.         $posts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
  17.         foreach($posts as $post) :
  18. ?>
  19. <url>
  20. <loc><?php echo get_permalink($post->ID); ?></loc>
  21. <lastmod><?php echo str_replace(" ", "T", get_post($post->ID)->post_modified); ?>+00:00</lastmod>
  22. <changefreq>monthly</changefreq>
  23. <priority>0.6</priority>
  24. </url>
  25. <?php
  26.         endforeach;
  27.         // 页面
  28.         $pages = get_pages('numberposts=-1&orderby=post_date&order=DESC');
  29.         foreach($pages as $page) :
  30. ?>
  31. <url>
  32. <loc><?php echo get_page_link($page->ID); ?></loc>
  33. <lastmod><?php echo str_replace(" ", "T", get_page($page->ID)->post_modified); ?>+00:00</lastmod>
  34. <changefreq>weekly</changefreq>
  35. <priority>0.6</priority>
  36. </url>
  37. <?php
  38.         endforeach;
  39.         // 分类
  40.         $categorys = get_terms('category', 'orderby=name&hide_empty=0');
  41.         foreach ($categorys as $category) :
  42. ?>
  43. <url>
  44. <loc><?php echo get_term_link($category, $category->slug); ?></loc>
  45. <changefreq>weekly</changefreq>
  46. <priority>0.8</priority>
  47. </url>
  48. <?php
  49.         endforeach;
  50.         // 标签
  51.         $tags = get_terms('post_tag', 'orderby=name&hide_empty=0');
  52.         foreach ($tags as $tag) :
  53. ?>
  54. <url>
  55. <loc><?php echo get_term_link($tag, $tag->slug); ?></loc>
  56. <changefreq>monthly</changefreq>
  57. <priority>0.4</priority>
  58. </url>
  59. <?php
  60.         endforeach;
  61. ?>
  62. </urlset>
  63. <?php
  64.         $sitemap = ob_get_contents();
  65.         ob_clean();
  66.         return $sitemap;
  67. }
复制代码


送给懒癌患者:丢到 主题目录中 modules 文件夹

链接:https://pan.baidu.com/s/1Q8r3DX0D6Uu0kVhRWdxLpg

提取码:72ep

复制这段内容后打开百度网盘手机App,操作更方便哦


如果您需要添加公告到sitemap中,请在</url>后  <?php  前加入以下代码:
  1. <?php
  2.         endforeach;
  3.         // 公告
  4.         $posts = get_posts('post_type=bulletin&numberposts=-1&orderby=post_date&order=DESC');
  5.         foreach($posts as $post) :
  6. ?>
  7. <?php if (_hui('no_bulletin')) { ?>
  8. <url>
  9. <loc><?php echo get_permalink($post->ID); ?></loc>
  10. <lastmod><?php echo str_replace(" ", "T", get_post($post->ID)->post_modified); ?>+00:00</lastmod>
  11. <changefreq>monthly</changefreq>
  12. <priority>0.6</priority>
  13. </url>
复制代码

DUX公告添加方法:https://cvps.top/9244.html  。

总结
此代码乃参考begin主题,然后删减得来,也就是现在本小站使用的主题集成的。

注意:请使用此方法后,到后台随意打开一个文章,然后进行编辑 =>更新 。这样就能更新sitemap.xml 了。

嘿嘿,标题就告诉您 更新文章时生成站点地图xml 了。


演示截图:
发表于 2019-5-11 21:06:57 来自手机 | 显示全部楼层
主题呢?有下载吗?
发表于 2019-5-11 21:09:55 | 显示全部楼层
其实谷歌sitemap很好用
 楼主| 发表于 2019-5-11 21:10:39 | 显示全部楼层
hxuf 发表于 2019-5-11 21:09
其实谷歌sitemap很好用

能用在百度上吗? 您说的这个是用插件吗?
发表于 2019-5-11 21:12:16 | 显示全部楼层
初缘Cvps小站 发表于 2019-5-11 21:10
能用在百度上吗? 您说的这个是用插件吗?

能的 而且非常好用那。可以按月。对于文章多的、宽带小的,效果非常好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-24 03:04 , Processed in 0.058200 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表