3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpressでプラグイン無しでサイトマップ(XML)を作る

Last updated at Posted at 2018-03-02

現在WordpressのAll in one SEOというプラグインの拡張機能でサイトマップを生成しているのだが、サイトマップ内のURLが投稿、カテゴリ、画像ファイルのリンクなど雑多になっているので、プラグイン無しでサイトマップを動的に生成する関数を作成してみた。

ここでは以下のリンクを参考に投稿、固定ページ、カテゴリ、画像ファイルのリンクを作成してみた。
https://web.contempo.jp/weblog/tips/p914

上記のリンクでは投稿、固定ページのサイトマップに対応しているので、カテゴリ、画像ファイルのサイトマップの関数をメモ。

関数はそれぞれfunctions.phpに記載していく。

カテゴリサイトマップ

sitemap_category.php
<?php
add_action( "create_category", "create_xml_sitemap_category" );
add_action( "delete_category", "create_xml_sitemap_category" );

function create_xml_sitemap_category() {
$args = array(
	'order' => 'DESC'
);
$sitemaps = '';
foreach( get_categories( $args ) as $post):
$sitemaps .= 
	"\t".'<url>'."\n".
    "\t\t".'<loc>'. get_category_link( $post->term_id ) .'</loc>'."\n".
    "\t\t".'<priority>1</priority>'."\n". //任意の優先度を入力
	"\t\t".'<changefreq>monthly</changefreq>'."\n". //任意の期間を入力
	"\t".'</url>'."\n";
endforeach; 

$sitemap = 
	'<?xml version="1.0" encoding="UTF-8"?>'."\n".
	'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n".
	$sitemaps. 
	'</urlset>'."\n";
$fopen = fopen( ABSPATH. "sitemap_category.xml", 'w' );
fwrite($fopen, $sitemap);
fclose($fopen);
}

画像サイトマップ

画像サイトマップの作成方法は以下を参照
https://support.google.com/webmasters/answer/178636?hl=ja

sitemap_image.php
add_action( "add_attachment", "create_xml_sitemap_image" );
add_action( "edit_attachment", "create_xml_sitemap_image" );

function create_xml_sitemap_image(){
$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => -1,
	'order' => 'DESC'
);

$sitemaps = '';
foreach(get_posts($args) as $post) :
   $caption = $post->post_excerpt ? $post->post_excerpt : ' ';
   $title = $post->post_title ? $post->post_title : ' ';

$sitemaps .= 
"\t".'<url>'."\n".
"\t\t".' <loc>'.get_attachment_link($post->ID).'</loc>'."\n".
"\t\t".'<image:image>'."\n".
"\t\t\t".'<image:loc>'.wp_get_attachment_url($post->ID).'</image:loc>'."\n".
"\t\t\t".'<image:caption>'. htmlspecialchars($caption) .'</image:caption>'."\n". //任意
"\t\t\t".'<image:title>'. htmlspecialchars($title) .'</image:title>'."\n". //任意
"\t\t".'</image:image>'."\n".
"\t".'</url>'."\n";
endforeach;

$sitemap = 
	'<?xml version="1.0" encoding="UTF-8"?>'."\n".
	'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n".
	$sitemaps. 
	'</urlset>'."\n";
$fopen = fopen( ABSPATH. "sitemap_image.xml", 'w' );
fwrite($fopen, $sitemap);
fclose($fopen);
};

後は https://ex.com/sitemap_category.xml, https://ex.com/sitemap_image.xml にそれぞれアクセスすると表示される。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?