質問編集履歴

4

文章中の説明を追加「テーマの機能(noindexのチェックボックスにチェックを入れる)」」

2018/12/09 14:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  function.phpに下記コードを記載して、記事更新時に自動的にサイトマップが送信されるようにしています。
4
4
 
5
- サイトの内容上、非常に短い文章など、中身が薄いページを多数掲載せざるを得ず、そういったページはテーマの機能を使い、ページ毎にnoindexを付けています。その結果、サーチコンソールにて「送信された URL に noindex タグが追加されています」とのエラーが大量になってしまいました。
5
+ サイトの内容上、非常に短い文章など、中身が薄いページを多数掲載せざるを得ず、そういったページはテーマの機能(noindexのチェックボックスにチェック入れる)を使い、ページ毎にnoindexを付けています。その結果、サーチコンソールにて「送信された URL に noindex タグが追加されています」とのエラーが大量になってしまいました。
6
6
 
7
7
 
8
8
 

3

リンク追加

2018/12/09 14:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  テーマは下記URLの有料テーマですが、テーマの範疇外なのでこちらに質問させていただきました。
10
10
 
11
- https://snow-monkey.2inc.org/
11
+ [https://snow-monkey.2inc.org/](https://snow-monkey.2inc.org/)
12
12
 
13
13
 
14
14
 

2

テーマの入手先を質問文に追加しました。

2018/12/09 13:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,15 @@
4
4
 
5
5
  サイトの内容上、非常に短い文章など、中身が薄いページを多数掲載せざるを得ず、そういったページはテーマの機能を使い、ページ毎にnoindexを付けています。その結果、サーチコンソールにて「送信された URL に noindex タグが追加されています」とのエラーが大量になってしまいました。
6
6
 
7
+
8
+
9
+ テーマは下記URLの有料テーマですが、テーマの範疇外なのでこちらに質問させていただきました。
10
+
11
+ https://snow-monkey.2inc.org/
12
+
13
+
14
+
7
- そのため、最初からnoindexを除外して送信したいのですが、可能でしょうか。ページ数が多く、これからも増えるため、ページIDごとの除外ではなく、noindexのページを自動的に除外できるとありがたいです。
15
+ 最初からnoindexを除外して送信したいのですが、可能でしょうか。ページ数が多く、これからも増えるため、ページIDごとの除外ではなく、noindexのページを自動的に除外できるとありがたいです。
8
16
 
9
17
  phpの知識が少なく、下記のコードはネットの情報を元に記載しています。
10
18
 

1

すみません、今稼働させているコードの全文を記載しました。サーチコンソールで確認すると、サイトマップは正常に送信されています。

2018/12/09 13:48

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -14,71 +14,131 @@
14
14
 
15
15
  これに投稿ステータスによりマップが作成される記載を加えています。
16
16
 
17
- ```php
17
+ ```ここに言語を入力
18
18
 
19
19
  function create_xml_sitemap() {
20
20
 
21
21
 
22
22
 
23
- $sitemap = <?xml version=1.0 encoding=UTF-8?> . \n .
23
+ $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
24
24
 
25
- <urlset xmlns=http://www.sitemaps.org/schemas/sitemap/0.9> . \n;
25
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
26
26
 
27
27
 
28
28
 
29
- $args = array(
29
+ $args = array(
30
30
 
31
- posts_per_page => -1,
31
+ 'posts_per_page' => -1,
32
32
 
33
- orderby => modified,
33
+ 'orderby' => 'modified',
34
34
 
35
- order => DESC,
35
+ 'order' => 'DESC',
36
36
 
37
- post_type => array(post,page,news),
37
+ 'post_type' => array('post','page','news_tokyo','news_hokkaido','contribution'),
38
38
 
39
- post_status => publish
39
+ 'post_status' => 'publish'
40
40
 
41
- );
41
+ );
42
42
 
43
- $posts_array = get_posts( $args );
43
+ $posts_array = get_posts( $args );
44
44
 
45
- foreach( $posts_array as $post) {
45
+ foreach( $posts_array as $post) {
46
46
 
47
- $post_modified = explode( , $post->post_modified);
47
+ $post_modified = explode(' ', $post->post_modified);
48
48
 
49
- $sitemap .=
49
+ $sitemap .=
50
50
 
51
- \t . <url> . \n .
51
+ "\t" . '<url>' . "\n" .
52
52
 
53
- \t\t . <loc> . get_permalink( $post->ID ) . </loc> . \n .
53
+ "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' . "\n" .
54
54
 
55
- \t\t . <lastmod> . $post_modified[0] . </lastmod> . \n .
55
+ "\t\t" . '<lastmod>' . $post_modified[0] . '</lastmod>' . "\n" .
56
56
 
57
- \t\t . <changefreq>weekly</changefreq> . \n .
57
+ "\t\t" . '<changefreq>weekly</changefreq>' . "\n" .
58
58
 
59
- \t\t . <priority>0.8</priority> . \n .
59
+ "\t\t" . '<priority>0.8</priority>' . "\n" .
60
60
 
61
+ "\t" . '</url>' . "\n";
62
+
63
+ }
64
+
61
- “\t . </url> . \n;
65
+ $sitemap .= '</urlset>' . "\n";
66
+
67
+
68
+
69
+ $fh = fopen( ABSPATH. "sitemap.xml", 'w' );
70
+
71
+ if ($fh) {
72
+
73
+ fwrite($fh, $sitemap);
74
+
75
+ fclose($fh);
76
+
77
+ // グーグルに更新したことを通知
78
+
79
+ ping_trans( 'http://google.com/ping?sitemap=' . esc_url( home_url('/') ) . 'sitemap.xml');
80
+
81
+ }
62
82
 
63
83
  }
64
84
 
65
- $sitemap .= ‘</urlset>’ . “\n”;
85
+ // 投稿ステータスが公開または更新でサイトマップを作成するようにする
86
+
87
+ add_action( "publish_post", "create_xml_sitemap" );
88
+
89
+ add_action( "publish_page", "create_xml_sitemap" );
90
+
91
+ add_action( "publish_news_tokyo", "create_xml_sitemap" );
92
+
93
+ add_action( "publish_news_hokkaido", "create_xml_sitemap" );
94
+
95
+ add_action( "publish_contribution", "create_xml_sitemap" );
66
96
 
67
97
 
68
98
 
69
- $fh = fopen( ABSPATH. “sitemap.xml”, ‘w’ );
99
+ // 投稿ステータスが公開または更新でサイトマップを作成するようにする
70
100
 
71
- if ($fh) {
101
+ function ping_trans($url) {
72
102
 
73
- fwrite($fh, $sitemap);
74
103
 
75
- fclose($fh);
76
104
 
77
- // グーグルに更新したことを通知
105
+ $ch = curl_init();
78
106
 
79
- ping_trans( ‘http://google.com/ping?sitemap= . esc_url( home_url(‘/’) ) . ‘sitemap.xml’);
107
+ if ($ch != false) {
80
108
 
109
+ curl_setopt($ch, CURLOPT_URL, $url);
110
+
111
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
112
+
113
+ $result= curl_exec($ch);
114
+
115
+ curl_close($ch);
116
+
117
+
118
+
119
+ // 送信結果をログファイルに保存
120
+
121
+ if ($result == false) {
122
+
123
+ $str = date_i18n("Y-m-d H:i:s") . ' send NG.' . "\n";
124
+
125
+ } else {
126
+
127
+ $str = date_i18n("Y-m-d H:i:s") . ' send OK.' . "\n";
128
+
81
- }
129
+ }
130
+
131
+ $fh = fopen( ABSPATH. "sitemap.log", 'a+' );
132
+
133
+ if ($fh) {
134
+
135
+ fwrite($fh, $str);
136
+
137
+ fclose($fh);
138
+
139
+ }
140
+
141
+ }
82
142
 
83
143
  }
84
144