theme development – How to add the URL and the modification date of the home page to sitemap.xml in wordpress?


I am using this code to add sitemap.xml to my wordpress site without plugin. it works very well. but is incomplete. please add the url and modification date of the wordpress home page (index.php) to this sitemap. I post my recent and recently modified publications on my home page.

function xml_sitemap() {
$postsForSitemap = get_posts(array('numberposts' => -1, 'orderby' => 'modified', 'post_type' => array('post'), 'order' => 'DESC'));
$sitemap = '';
$sitemap. = '';
foreach($postsForSitemap as $post) {
    setup_postdata($post);
    $sitemap. = ''.
    ''.get_permalink($post - > ID).
    ''.
    ''.get_the_modified_date('c', $post - > ID).
    ''.
    ''
}
$sitemap. = '';
$fp = fopen(ABSPATH.
    "sitemap.xml", 'w');
fwrite($fp, $sitemap);
fclose($fp)

} add_action ("publish_post", "xml_sitemap")