Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Interception de la requête "sitemap-news.xml"
- // Grâce à une fonction sur le filtre "template_include"
- add_filter( 'template_include', 'devblog_fr_sitemap_news', 1 );
- function devblog_fr_sitemap_news( $template ) {
- // Si l'url est en 404 (n'existe pas) et contien l'url de notre sitemap
- // Nous déclenchons notre fonction d'affichage.
- if( is_404() && strpos( $_SERVER['REQUEST_URI'], 'sitemap-news.xml' ) !== false )
- devblog_display_sitemap_news();
- return $template;
- }
- function devblog_display_sitemap_news(){
- // Nous allons rendre directement le contenu du sitemap
- // plutôt qu'un fichier.
- // Le contenu sera l'ensemble des articles publiés depuis moins de
- // 48 heures conformément à la recommandation de Google.
- header("HTTP/1.1 200 OK");
- nocache_headers();
- header("Content-type: text/xml");
- echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
- echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">'.PHP_EOL;
- $args = array(
- 'post_type' => 'post',
- 'post_statys' => 'publish',
- 'date_query' => array(
- array(
- 'column' => 'post_modified_gmt',
- 'after' => '2 day ago',
- ),
- ),
- 'posts_per_page' => -1,
- );
- $query = new WP_Query( $args );
- if( !empty( $query->posts ) ) :
- foreach ( $query->posts as $key => $post) {
- echo '<url>'.PHP_EOL;
- echo '<loc>'.trailingslashit( get_permalink( $post->ID ) ).'</loc>'.PHP_EOL;
- echo '<news:news>'.PHP_EOL;
- echo '<news:publication>'.PHP_EOL;
- echo '<news:name>'.get_bloginfo('name').'</news:name>'.PHP_EOL;
- echo '<news:language>'.substr( get_bloginfo('language'), 0, 2).'</news:language>'.PHP_EOL;
- echo '</news:publication>'.PHP_EOL;
- echo '<news:publication_date>'.get_the_date('c', $post->ID ).'</news:publication_date>'.PHP_EOL;
- echo '<news:title>'.get_the_title($post->ID).'</news:title>'.PHP_EOL;
- echo '</news:news>'.PHP_EOL;
- echo '</url>'.PHP_EOL;
- }
- endif;
- echo '</urlset>'.PHP_EOL;
- exit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement