Сайтмапа для фидов. Шаблон template-html-sitemap.php заливать в корень темы!!!
<?php
/**
* Template Name: HTML Sitemap Page
*
*/
$sitemapId = get_query_var('sitemapOffset');
$step = 1000;
$args = [
'posts_per_page' => $step,
'post_status' => 'publish',
'post_type' => ['post'],
'category__in' => [45] // id category
];
/** @var WP_Query $data */
$timeRoundToMidnight = strtotime('today', $sitemapId);
$data = $sitemapId ? new WP_Query(array_merge($args, [
'date_query' => [
[
'before' => date('Y-m-d', $timeRoundToMidnight),
'inclusive' => true,
],
],
])) : call_user_func(function () use ($args, $step){
$count = (wp_count_posts('news'))->publish / $step;
$wpQuery = new WP_Query();
$tempWPQuery = array_values(array_filter(array_map(function ($count) use ($args){
return current((new WP_Query(array_merge($args, [
'posts_per_page' => 1,
'offset' => $count,
])))->posts);
}, range(0, ceil($count) * $step, $step))));
$wpQuery->posts = $tempWPQuery;
$wpQuery->post_count = count($tempWPQuery);
return $wpQuery;
});
header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php if ($sitemapId) { ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-news/0.9 http://www.google.com/schemas/sitemap-news/0.9/sitemap-news.xsd">
<?php } else { ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php } ?>
<?php
function setPosts($post)
{ ?>
<url>
<loc><?php echo get_the_permalink(); ?></loc>
<news:news>
<news:publication>
<news:name>Organization</news:name>
<news:language>pl</news:language>
</news:publication>
<news:publication_date><?php echo get_the_date('Y-m-d'); ?></news:publication_date>
<news:title><?php echo get_the_title(); ?></news:title>
</news:news>
</url>
<?php
}
function setSitemaps($datum)
{
global $wp;
?>
<sitemap>
<loc><?php echo home_url($wp->request . '/') . get_post_time(); ?>/</loc>
<lastmod><?php echo date('Y-m-d', get_post_time()); ?></lastmod>
</sitemap>
<?php
}
if ($data->have_posts()) {
while ($data->have_posts()) {
$data->the_post();
$sitemapId ? setPosts($data) : setSitemaps($data);
}
}
?>
<?php if (!$sitemapId) { ?>
</sitemapindex>
<?php } else { ?>
</urlset>
<?php } ?>
<?php
/**
* Template Name: Functions
*/
add_filter('wp', function () {
global $wp_query;
$urlArray = array_values(array_filter(explode('/', $_SERVER['REQUEST_URI'])));
if (!empty($urlArray[0]) && $urlArray[0] === 'news-sitemap') {
if (substr($_SERVER['REQUEST_URI'], -1) !== '/') {
wp_redirect($_SERVER['REQUEST_URI'] . '/');
exit;
}
set_query_var('sitemapOffset', (int)($urlArray[1] ?? 0));
status_header(200);
$wp_query->is_404 = false;
get_template_part('template-html-sitemap');
exit;
}
});
//Если сайтмапа не в корневой язык
add_filter('wp', function () {
global $wp_query;
$urlArray = array_values(array_filter(explode('/', $_SERVER['REQUEST_URI'])));
if (pll_current_language() === 'en'){
array_unshift($urlArray, 'en');
}
if (!empty($urlArray[1]) && $urlArray[1] === 'news-sitemap') {
if (substr($_SERVER['REQUEST_URI'], -1) !== '/') {
wp_redirect($_SERVER['REQUEST_URI'] . '/');
exit;
}
set_query_var('sitemapOffset', (int)($urlArray[2] ?? 0));
status_header(200);
$wp_query->is_404 = false;
get_template_part('template-html-sitemap');
exit;
}
});
//Убрать 301 с комментов
add_action( 'after_setup_theme', 'head_cleanup' );
function head_cleanup(){
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// disable comments feed
add_filter( 'feed_links_show_comments_feed', '__return_false' );
}
?>