Skip to content

Instantly share code, notes, and snippets.

@seoagentur-hamburg
Last active December 29, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seoagentur-hamburg/0a5e335741048f135286653a6dfdca91 to your computer and use it in GitHub Desktop.
Save seoagentur-hamburg/0a5e335741048f135286653a6dfdca91 to your computer and use it in GitHub Desktop.
Erstellt eine sitemap.xml aus eigenen Mitteln ohne Plugin
<?php
/**
* Eine eigene sitemap.xml ohne Plugin erstellen
* @author Andreas Hecht
*/
function ah_create_sitemap() {
$sitemap_posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'modified',
'post_type' => array('post','page'), // Deine Custom Post Types hier einfügen (z.B. Portfolio)
'order' => 'DESC'
));
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($sitemap_posts as $post) {
setup_postdata($post);
$postdate = explode(" ", $post->post_modified);
$sitemap .= '<url>'.
'<loc>'. get_permalink($post->ID) .'</loc>'.
'<lastmod>'. $postdate[0] .'</lastmod>'.
'<changefreq>monthly</changefreq>'.
'</url>';
}
$sitemap .= '</urlset>';
$fp = fopen(ABSPATH . "sitemap.xml", 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
add_action('publish_post', 'ah_create_sitemap');
add_action('publish_page', 'ah_create_sitemap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment