回答編集履歴

1

ソースコード追記

2017/01/06 11:18

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,41 @@
1
1
  `$postsYear`に入っている値が2017ではなく、2016になっているのではないですか?
2
+
3
+
4
+
5
+ ###追記
6
+
7
+ `$postYear`取得コードを以下のコードに置き換えてください。
8
+
9
+ 最新記事の投稿年を取得できます。
10
+
11
+ ```PHP
12
+
13
+ // 投稿タイプ'post'の記事を日付順(新しい順)に取得する
14
+
15
+ $args = array( 'orderby'=>'date', 'order'=>'DESC', 'post_type'=>'post' );
16
+
17
+ $postlist = get_posts($args);
18
+
19
+
20
+
21
+ // 一番新しい記事の投稿日を取得し、タイムスタンプに変換
22
+
23
+ $date = strtotime($postlist[0]->post_date);
24
+
25
+
26
+
27
+ // タイムスタンプから「年」だけを取得
28
+
29
+ $postYear = date('Y', $date);
30
+
31
+ ```
32
+
33
+ **参考URL**
34
+
35
+ [テンプレートタグ/get posts - WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/get_posts)
36
+
37
+ [クラスリファレンス/WP Post - WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/WP_Post)
38
+
39
+ [PHP: strtotime - Manual](http://php.net/manual/ja/function.strtotime.php)
40
+
41
+ [PHP: date- Manual](http://php.net/manual/ja/function.date.php)