需求
目前站点是点击“发布”按钮后生成sitemap.xml
实现python调用api接口发布文章后,也能够正常生成sitemap.xml
钩子
将发布文章的钩子save_post
调整为publish_post
```php
add_action("publish_post", "eg_create_sitemap");
function eg_create_sitemap() {
if(str_replace('-', '', get_option('gmt_offset'))<10) { $tempo = '-0'.str_replace('-', '', get_option('gmt_offset')); } else { $tempo = get_option('gmt_offset'); }
if(strlen($tempo)==3) { $tempo = $tempo.':00'; }
$postsForSitemap = get_posts(array(
'numberposts' => -1,
'orderby' => 'modified',
'post_type' => array('post','page'),
'order'=> 'DESC'));
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= "\n".'<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
$sitemap .= '<!-- generated-on=' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '-->'."\n";
$sitemap .= "\t".'<url>'."\n".
"\t\t".'<loc>'. esc_url( home_url( '/' ) ) .'</loc>'.
"\n\t\t".'<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '</lastmod>'.
"\n\t\t".'<changefreq>daily</changefreq>'.
"\n\t\t".'<priority>1.0</priority>'.
"\n\t".'</url>'."\n";
foreach($postsForSitemap as $post) {
setup_postdata($post);
$postdate = explode(" ", $post->post_modified);
$sitemap .= "\t".'<url>'."\n".
"\t\t".'<loc>'. get_permalink($post->ID) .'</loc>'.
"\n\t\t".'<lastmod>'. $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>'.
"\n\t\t".'<changefreq>Weekly</changefreq>'.
"\n\t\t".'<priority>0.5</priority>'.
"\n\t".'</url>'."\n";
}
$sitemap .= '</urlset>';
$fp = fopen(ABSPATH . "sitemap.xml", 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
```
当前文章价值4.28元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)
评论已关闭!