teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

コードの各箇所にコメント追加

2021/06/19 08:39

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -36,15 +36,21 @@
36
36
  <?php
37
37
  $urilist = explode("/",$_SERVER['REQUEST_URI']);
38
38
  $selected_check = false;
39
+
40
+ //3番目が年、4番目が月。
41
+ //どちらもない場合は /news/ だけと判定できるのでそもそもselectedの判定をしない
39
42
  if(isset($urilist[3]) && isset($urilist[4])){
40
43
  $selected_check = true;
41
44
  }
45
+
46
+ // $archive_list をループ
42
47
  foreach ($archive_list as $year_month => $archive) :
43
- $selected = '';
48
+ $selected = ''; //初期化(合致しないときにはselectedを出さない)
44
49
  if($selected_check && $year_month === ($urilist[3].'/'.$urilist[4])){
50
+ //$year_monthがURLにあるものと合致していればselected
45
51
  $selected = ' selected';
46
52
  }
47
- $year_month_arr = explode('/', $year_month);
53
+ $year_month_arr = explode('/', $year_month); //これは元のコードです。
48
54
  ?>
49
55
 
50
56
 

3

短縮構文修正

2021/06/19 08:39

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -48,7 +48,7 @@
48
48
  ?>
49
49
 
50
50
 
51
- <option value="<?php echo esc_url(home_url('/news/date/' . $year_month . '/' . '?' . $taxonomy_slug . '=' . $term)) ?>"<?php=$selected ?>>
51
+ <option value="<?php echo esc_url(home_url('/news/date/' . $year_month . '/' . '?' . $taxonomy_slug . '=' . $term)) ?>"<?=$selected ?>>
52
52
 
53
53
 
54
54
  ```

2

URIのチェックを外だし。

2021/06/19 06:00

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -35,9 +35,13 @@
35
35
 
36
36
  <?php
37
37
  $urilist = explode("/",$_SERVER['REQUEST_URI']);
38
+ $selected_check = false;
39
+ if(isset($urilist[3]) && isset($urilist[4])){
40
+ $selected_check = true;
41
+ }
38
42
  foreach ($archive_list as $year_month => $archive) :
39
43
  $selected = '';
40
- if(isset($urilist[3]) && isset($urilist[4]) && $year_month === ($urilist[3].'/'.$urilist[4])){
44
+ if($selected_check && $year_month === ($urilist[3].'/'.$urilist[4])){
41
45
  $selected = ' selected';
42
46
  }
43
47
  $year_month_arr = explode('/', $year_month);

1

edit

2021/06/19 05:26

投稿

m.ts10806
m.ts10806

スコア80888

answer CHANGED
@@ -26,4 +26,25 @@
26
26
  URLを見る限り、年と月の順番は決まってるみたいなので、
27
27
  月が替わっても同じ順番になるものと思われます。
28
28
 
29
- そこでその配列の番号を指定して年月を取得し同じならselectedをつけるとかで対応できると思います([isset()](https://www.php.net/manual/ja/function.isset.php)とかで確認して存在しなければ「全て」ですね)
29
+ そこでその配列の番号を指定して年月を取得し同じならselectedをつけるとかで対応できると思います([isset()](https://www.php.net/manual/ja/function.isset.php)とかで確認して存在しなければ「全て」ですね)
30
+
31
+ ---
32
+
33
+ WordPress環境手元にないので未検証ですが、このような形で。
34
+ ```php
35
+
36
+ <?php
37
+ $urilist = explode("/",$_SERVER['REQUEST_URI']);
38
+ foreach ($archive_list as $year_month => $archive) :
39
+ $selected = '';
40
+ if(isset($urilist[3]) && isset($urilist[4]) && $year_month === ($urilist[3].'/'.$urilist[4])){
41
+ $selected = ' selected';
42
+ }
43
+ $year_month_arr = explode('/', $year_month);
44
+ ?>
45
+
46
+
47
+ <option value="<?php echo esc_url(home_url('/news/date/' . $year_month . '/' . '?' . $taxonomy_slug . '=' . $term)) ?>"<?php=$selected ?>>
48
+
49
+
50
+ ```