Last active
December 29, 2016 14:09
-
-
Save seoagentur-hamburg/0a5e335741048f135286653a6dfdca91 to your computer and use it in GitHub Desktop.
Erstellt eine sitemap.xml aus eigenen Mitteln ohne Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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