작성
·
18
0
사이트/sitemap.xml
로 접속시에 wp-sitemap.xml로 리다이렉트 없이 This XML file does not appear to have any style information associated with it. The document tree is shown below.
문구와 함께
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>...
</url>
</urlset>
가 화면에 표시됩니다.
사이트/wp-sitemap.xml로 직접 타이핑해서 들어가면 화면에
표시됩니다.
콘솔에는
출력되고 있습니다.
제공해주신 generatepress_child.zip을 그대로 사용중인데 원인을 잘 모르겠네요.
답변 1
0
안녕하세요, 재훈님.
강의 첨부파일로 제공되는 generatepress_child.zip 에서는 /sitemap.xml 을 그냥 사용하게 작성되어있습니다.
// 기존 wp-sitemap.xml 비활성화
add_filter('wp_sitemaps_enabled', '__return_false');
// /sitemap.xml 커스텀 sitemap 라우팅
add_action('init', function () {
add_rewrite_rule('^sitemap\.xml$', 'index.php?custom_sitemap=1', 'top');
});
functions.php 파일을 보면 위의 코드에서 처럼 원래 제공하던 wp-sitemap.xml 을 비활성화 하고 sitemap.xml 을 동작하게 수정되었으며,
// sitemap.xml 응답 처리
add_action('template_redirect', function () {
if (intval(get_query_var('custom_sitemap')) === 1) {
header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'modified',
'order' => 'DESC'
];
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
$url = get_permalink();
$lastmod = get_the_modified_time('Y-m-d\TH:i:sP');
echo "<url>\n";
echo "<loc>{$url}</loc>\n";
echo "<lastmod>{$lastmod}</lastmod>\n";
echo "<changefreq>weekly</changefreq>\n";
echo "<priority>0.8</priority>\n";
echo "</url>\n";
}
wp_reset_postdata();
echo '</urlset>';
exit;
}
});
실제 sitemap.xml 이 호출될때 위의 코드가 동작하게 되어있는데, 내용을 보면 아주 단순하게 작성된 포스트만 목록화 해서 출력되게 수정된 버전입니다.
이는 기본적으로 제공되는 wp-sitemap.xml 이 기능상으로는 체계적으로 작성되어있긴 한데 블로그 운영 초반 포스트 위주로 운영할 시에는 오히려 검색 노출에 방해가 되는 요소들이 많아 그렇게 수정하여 적용하고 있으니 참고가 되셨길 바랍니다. 위의 코드를 제거하거나 주석처리 하면 원래 기본적으로 제공되는 wp-sitemap.xml 이 동작하긴 합니다.
감사합니다!