Advertisement
Guest User

template-google-news.php

a guest
Nov 19th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Google News Sitemap
  4. */
  5. define('DONOTCACHEPAGE', true);
  6. define('DONOTCACHEDB', true);
  7. define('DONOTCACHCEOBJECT', true);
  8. header("Content-type: text/xml");
  9. echo '<?xml version="1.0" encoding="UTF-8"?>
  10. <?xml-stylesheet type="text/xsl" href="https://www.racunalo.com/google-news-sitemap.xsl"?>';
  11. echo '
  12. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">';
  13. $have_posts = false;
  14. $args_news_sitemap = array (
  15.     'orderby' => 'date',
  16.     'order' => 'DESC',
  17.     'date_query' => array(
  18.         'after' => date('Y-m-d', strtotime('-2 days'))
  19.     ),
  20.     'category__not_in' => array(1),
  21.     'showposts' => 100,
  22.     'post_status' => 'publish',
  23.     'post_type'=>'post',
  24.     'no_found_rows' => true,
  25.     'cache_results' => false,
  26.     'update_post_term_cache' => false,
  27.     'update_post_meta_cache' => false
  28. );
  29. $query_news_sitemap = new WP_Query($args_news_sitemap);
  30. if($query_news_sitemap->have_posts()):
  31.     while($query_news_sitemap->have_posts()):
  32.         $query_news_sitemap->the_post();
  33.         $have_posts = true;
  34.     echo '
  35.    <url>';
  36.         echo '
  37.        <loc>';
  38.         echo the_permalink();
  39.         echo '</loc>';
  40.         echo '
  41.        <news:news>
  42.            <news:publication>
  43.                <news:name>Website.com</news:name>
  44.                <news:language>en</news:language>
  45.            </news:publication>
  46.            <news:publication_date>';
  47.             echo the_time('c');
  48.             echo '</news:publication_date>
  49.            <news:title>';
  50.             echo the_title_attribute('echo=0');
  51.             echo '</news:title>
  52.            <news:keywords>News</news:keywords>
  53.        </news:news>
  54.    </url>
  55. ';
  56.     endwhile;
  57. endif;
  58. if (!$have_posts):
  59.     echo '<url>
  60.        <loc>https://www.website.com/</loc>
  61.    </url>';
  62. endif; wp_reset_query();
  63. echo '
  64. </urlset>';
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement