WordPress生成站点地图xml 代码版,适合内容较少的用户选择
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

WordPress插件是非常丰富的,很多功能可以通过插件来实现,不过插件较多会影响一定的速度以及不同主题模板之间的兼容性。站点地图不少使用wordpress程序用户要使用的,不过需要安装相应的插件,小七博客在之前分享过wordpress sitemap插件,不过也可以使用代码来实现,以下方法适合DUX主题。其他主题自行修改相对位置位置,谢谢。
1、在主题目录中options.php 添加开关。
复制 $options[] = array( 'name' => __('更新文章时生成站点地图xml', 'haoui'), 'desc' => __('开启', 'haoui'), 'id' => 'sitemap_xml', 'std' => true, 'type' => 'checkbox');注:位置自己安排,没有要求!
2、在主题目录 functions.php 添加
复制// sitemap_xmlif (_hui('sitemap_xml')) { function cvps_sitemap_resh() { require_once get_template_directory() . '/modules/sitemap-xml.php'; $sitemap_xml = cvps_get_xml_sitemap(); file_put_contents(ABSPATH.'sitemap.xml', $sitemap_xml); } if ( defined('ABSPATH') ) { add_action('publish_post', 'cvps_sitemap_resh'); add_action('save_post', 'cvps_sitemap_resh'); add_action('edit_post', 'cvps_sitemap_resh'); add_action('delete_post', 'cvps_sitemap_resh'); }}3、在主题目录中 modules 文件夹 新建 sitemap-xml.php 并添加以下代码:
复制复制<?php function cvps_get_xml_sitemap() { ob_start(); echo '<?xml version="1.0" encoding="UTF-8"?>';?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"><!-- generated-on=<?php echo get_lastpostdate('blog'); ?> --><url><loc><?php echo get_home_url(); ?></loc><lastmod><?php echo gmdate('Y-m-d\TH:i:s+00:00', strtotime(get_lastpostmodified('GMT'))); ?></lastmod><changefreq>daily</changefreq><priority>1.0</priority></url><?php // 文章 $posts = get_posts('numberposts=-1&orderby=post_date&order=DESC'); foreach($posts as $post) : ?><url><loc><?php echo get_permalink($post->ID); ?></loc><lastmod><?php echo str_replace(" ", "T", get_post($post->ID)->post_modified); ?>+00:00</lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url><?php endforeach; // 页面 $pages = get_pages('numberposts=-1&orderby=post_date&order=DESC'); foreach($pages 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 endforeach; // 分类 $categorys = get_terms('category', 'orderby=name&hide_empty=0'); foreach ($categorys as $category) : ?><url><loc><?php echo get_term_link($category, $category->slug); ?></loc><changefreq>weekly</changefreq><priority>0.8</priority></url><?php endforeach; // 标签 $tags = get_terms('post_tag', 'orderby=name&hide_empty=0'); foreach ($tags as $tag) : ?><url><loc><?php echo get_term_link($tag, $tag->slug); ?></loc><changefreq>monthly</changefreq><priority>0.4</priority></url><?php endforeach;?></urlset><?php $sitemap = ob_get_contents(); ob_clean(); return $sitemap;}如果您需要添加公告到sitemap中,请在</url>后 <?php 前加入以下代码:
复制<?php endforeach; // 公告 $posts = get_posts('post_type=bulletin&numberposts=-1&orderby=post_date&order=DESC'); foreach($posts as $post) : ?><?php if (_hui('no_bulletin')) { ?><url><loc><?php echo get_permalink($post->ID); ?></loc><lastmod><?php echo str_replace(" ", "T", get_post($post->ID)->post_modified); ?>+00:00</lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>注意事项:此方法适合文章内容在1000之内的,超过生成时间较长!
[wordpress教程]历史优惠活动内容
猜你可能想看的VPS
- 高速 tmhhost→1G 1 核 20G 15M 1T 流量 季付¥9全球[VPS测评]
- drServer→$56 月-E 2134 16GB 480GB SSD全球[VPS测评]
- 美国虚拟主机排名推荐 TOP 榜美国VPS[主机]
- 腾讯云海外免备案云服务器 2 折 318 元 年起 香港韩国日本超低延迟日本VPS[主机]
- 文件夹选项在哪?文件夹选项怎么打开?全球[VPS测评]
- STSDUST→$86 月 4 核 2B 内存 20GB 空间 20TB虚拟空间(主机)
- DMIT 美国 CN2 特价 VPS→原生 IP 可 Netflix 1美国VPS[主机]
- $3.5 月 1G 内存 50G 硬盘 1T 流量 1Gbps KVM 全球[VPS测评]
- 疯狂猜成语 图猜成语一个饼字一个几字中间有一只毛笔是什么成语?全球[VPS测评]
- 在 Ubuntu 系统下安装最新稳定版 qBittorrent全球[VPS测评]
- stsdust 广州移动 vds 预售,1Gbps 带宽无限流量,硬件资全球[VPS测评]
- 价格过高-Wired Blade→$5 月 2GB 内存 20GB NV虚拟空间(主机)
- Evolution Host→€5 月 1GB 内存 20GB SSD 虚拟空间(主机)
- 搬瓦工VPS面板新增Mount ISO内置100+系统镜像 附安装Gno全球[VPS测评]
- ServerGlobalServices → 6.5$月付 2C2G30全球[VPS测评]
- DigitalVirt → 月付15起 美国洛杉矶VPS 1核1G内存5美国VPS[主机]
- Cloudcone → 512M 10G SSD 1T $2.22月付全球[VPS测评]
- Virmach 黑五及超级星期一活动全球[VPS测评]
- 云计算拯救互联网巨头:谁抢跑,谁追赶?全球[VPS测评]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 邢志宏:加快推动高质量建设跨境电商综试区起势成势全球[VPS测评]
- 八成企业看好出海投资 防止“溺水”需重视这一点全球[VPS测评]
- 品光网络怎么样?香港美国服务器/云/高防,开年采购3.5折起美国VPS[主机]
- RFCHost:1核1G洛杉矶vps/15GB空间/1TB流量/KVM/虚拟空间(主机)
- 性价比最高的VPS网络评测全球[VPS测评]
- 众创逸云:1核2G云服务器,不限流量vps/5Mbps/KVM/香港CN香港VPS[主机]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- DiyVM:香港/日本/美国vps低至5折,2G内存月付69元起;香港服日本VPS[主机]
- 云服务器和服务器各有什么优缺点?全球[VPS测评]
- 桔子数据,香港安畅BGP大带宽VPS 首月五折体验 三网直连 14元/月香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-73520.html











