python成功发布文章后触发生成sitemap

2024-02-28 22:08 python成功发布文章后触发生成sitemap已关闭评论

需求

目前站点是点击“发布”按钮后生成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元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://www.teachcourse.cn/3083.html ,谢谢支持!

资源分享

分类:xampp 标签:
浅谈SQLite数据库的操作 浅谈SQLite数据库的操作
mysql数据库导出和导入 mysql数据库导出和导入
python库tkinter实现选择多文件上传,文本框显示命令行输出内容 python库tkinter实现选择多文件上
”Found 2 version of android-support-v4.jar in the dependency list“解决思路 ”Found 2 version of android-

评论已关闭!