質問編集履歴

1

forachにselect-box ・ $selected を追加し url から年4桁を抜き出すことで対応

2019/12/16 12:04

投稿

hakutou5963
hakutou5963

スコア15

test CHANGED
File without changes
test CHANGED
@@ -92,25 +92,31 @@
92
92
 
93
93
 
94
94
 
95
- if ( $arcresults ) {
95
+ if ( $arcresults ) {
96
96
 
97
- $afterafter = $after;
97
+ $defAfter = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
98
98
 
99
- foreach ( (array) $arcresults as $arcresult ) {
99
+ $After = preg_replace('/[^0-9]/', '', $defAfter);
100
100
 
101
- $url = add_query_arg( array( 'meta_key' => $date_field ), get_year_link( $arcresult->year ) );
101
+ foreach ( (array) $arcresults as $arcresult ) {
102
102
 
103
- $text = sprintf( '%d', $arcresult->year );
103
+ $After = substr($defAfter, 29, 4); // URLから年4桁を抜き出す
104
104
 
105
- if ($show_post_count)
105
+ $Years = $arcresult->year;
106
106
 
107
- $after = ' ('.$arcresult->posts.')' . $afterafter;
107
+ $Cnt = $arcresult->posts;
108
108
 
109
- $output .= get_archives_link( $url, $text, $format, $before, $after );
109
+ $selected = $Years === $After ? ' selected' : '';
110
110
 
111
- }
111
+ $url = add_query_arg( array( 'meta_key' => $date_field ), get_year_link( $arcresult->year ) );
112
112
 
113
+ $text = sprintf( '%d', $arcresult->year ).'年';
114
+
115
+ $output .= '<option value="'.$url.'"'.$selected.'>'.$text.' ('.$Cnt.')</option>';
116
+
113
- }
117
+ }
118
+
119
+ }
114
120
 
115
121
 
116
122