#DUX# WordPress 更新文章时生成站点地图xml 自动更新 代码版 (sitemap.xml)

简介

演示地址:本站:https://cvps.top/sitemap.xml

演示截图:

#DUX#  WordPress 更新文章时生成站点地图xml 自动更新 代码版 (sitemap.xml)

方法

以下方法适合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 并添加以下代码:

由于代码过长,请点击 [s][p]

  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. }

[/p]

送给懒癌患者:丢到 主题目录中 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主题,然后删减得来,也就是现在本小站使用的主题集成的。

[mark_b]

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

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

[/mark_b]

声明: 1.本站为个人非盈利站点,旨在个人学习、欣赏及记录等,故不受狭义的商业性版权限制,除非特别声明; 2.本站主要内容来源为本站编辑撰写、网友投稿(包括原创及非原创)、翻译外文和转载其他网站。
WordPress

wordpress #DUX主题# 首页文章特色图片,随机图片。 首页缩略图,无图时随机图片 方法

2019-5-8 22:45:16

WordPress

WordPress大前端主题【DUX5.3】记录增强内容

2019-5-11 23:25:18

搜索