当前位置:网站首页>WordPress opens sitemap XML, a piece of code to solve.
WordPress opens sitemap XML, a piece of code to solve.
2022-07-21 04:01:00 【WordPress applet development】
Everyone knows the website map , Baidu is not easy to climb your website without this , It is also difficult to be included , It happened that today I made a new website and shared it with you .
Just in your WordPress Add the following code to the root directory of the website , Create a new one PHP file , Name it sitemap.php
<?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/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By Zhang Ge's blog (https://zhang.ge)-->
<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
/* Article page */
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* The article cycle ends */ ?>
<?php
/* Single page */
$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 }} /* The single page cycle ends */ ?>
<?php
/* Blog classification */
$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.8</priority>
</url>
<?php }} /* Classification cycle ends */?>
<?php
/* label ( Optional ) */
$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>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php } /* End of label cycle */ ?>
</urlset>
If your server makes pseudo static rules, you need to add the following paragraph in pseudo static
If it is Nginx restart nginx that will do
rewrite ^/sitemap.xml$ /sitemap.php last;
If it is Apache restart Apache that will do
RewriteRule ^(sitemap)\.xml$ $1.php
And then visit https:// Your domain name /sitemap.xml
边栏推荐
- UGUI——RectMask2D
- 结合源码看《我所理解的cocos2dx-3.0》—— 渲染流程
- RIoTBoard开发板系列笔记(四)—— 使用VPU硬件解码
- SSM框架总和前的所有框架复习
- STM32CubeMX的CAN总线波特率设置
- stm32板间串口通信escape协议
- STM32CubeMX的正交编码器encoder
- Unity2D 自定义Scriptable Tiles的理解与使用(二)——通过了解Tile类来对TileBase类的方法(主要是GetTileData方法)进行详细理解
- Model graduation thesis of Landscape Architecture
- RIoTBoard开发板系列笔记(七)—— Framebuffer的使用
猜你喜欢
随机推荐
Chapter003 FPGA learning PWM LED breathing lamp
RIoTBoard开发板系列笔记(八)—— 构建桌面系统
From buying servers to building personal blog websites | detailed process of graphics and text (Tencent cloud | pagoda panel | WordPress | argon)
香蕉派 BPI-M5折腾记录(2)—— 编译u-boot
建筑装饰毕业论文题目
pinctrl
uni-app - App 平台内嵌网页物理手机自带返回键失效解决方案(内嵌的 webview 网页 H5 打包后手机物理返回键无效直接退出应用了)
Unity2D 自定义Scriptable Tiles的理解与使用(一)——了解TileBase类
解决错误:could not find ‘xxxTest‘
城市规划设计毕业论文范文
Model graduation thesis on traffic safety management
eslint报错
针对嵌入式设备外接设备的驱动开发心得
Chapter002-FPGA学习之按键控制LED灯和蜂鸣器
Qu'est - ce qu'un tuyau de rendu et comment dessiner un objet 3D
Clickhouse failed to start_ Unit clickhouse-server. service entered failed state
Markdown中编辑数学公式
Off chip spiflash embedded in FatFs based on stm32cubemx
UGUI——RectMask2D
Chinese NLP toolkits Chinese NLP tools