回答編集履歴
1
ソースコード追記
answer
CHANGED
@@ -1,1 +1,21 @@
|
|
1
|
-
`$postsYear`に入っている値が2017ではなく、2016になっているのではないですか?
|
1
|
+
`$postsYear`に入っている値が2017ではなく、2016になっているのではないですか?
|
2
|
+
|
3
|
+
###追記
|
4
|
+
`$postYear`取得コードを以下のコードに置き換えてください。
|
5
|
+
最新記事の投稿年を取得できます。
|
6
|
+
```PHP
|
7
|
+
// 投稿タイプ'post'の記事を日付順(新しい順)に取得する
|
8
|
+
$args = array( 'orderby'=>'date', 'order'=>'DESC', 'post_type'=>'post' );
|
9
|
+
$postlist = get_posts($args);
|
10
|
+
|
11
|
+
// 一番新しい記事の投稿日を取得し、タイムスタンプに変換
|
12
|
+
$date = strtotime($postlist[0]->post_date);
|
13
|
+
|
14
|
+
// タイムスタンプから「年」だけを取得
|
15
|
+
$postYear = date('Y', $date);
|
16
|
+
```
|
17
|
+
**参考URL**
|
18
|
+
[テンプレートタグ/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)
|
19
|
+
[クラスリファレンス/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)
|
20
|
+
[PHP: strtotime - Manual](http://php.net/manual/ja/function.strtotime.php)
|
21
|
+
[PHP: date- Manual](http://php.net/manual/ja/function.date.php)
|