回答編集履歴
1
ソースコード追記\(1\)
test
CHANGED
@@ -33,3 +33,223 @@
|
|
33
33
|
add_filter( 'getarchives_where', 'my_getarchives_where' );
|
34
34
|
|
35
35
|
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
###追記
|
40
|
+
|
41
|
+
元の`wp_get_archives()`の仕様をなるべく活かすように改造したら長くなりました。
|
42
|
+
|
43
|
+
テストはあまりしていませんが、多分動くと思います。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
- **`wp_get_archives()`の'post_type'パラメータが投稿タイプ名の配列を受け付けるように改造**
|
48
|
+
|
49
|
+
- **年月アーカイブページで複数の投稿タイプを表示できるようにクエリを追加**
|
50
|
+
|
51
|
+
####アーカイブリストを表示
|
52
|
+
|
53
|
+
```PHP
|
54
|
+
|
55
|
+
<?php
|
56
|
+
|
57
|
+
$args = array(
|
58
|
+
|
59
|
+
'type' => 'monthly',
|
60
|
+
|
61
|
+
'limit' => '',
|
62
|
+
|
63
|
+
'format' => 'html',
|
64
|
+
|
65
|
+
'before' => '',
|
66
|
+
|
67
|
+
'after' => '',
|
68
|
+
|
69
|
+
'show_post_count' => false,
|
70
|
+
|
71
|
+
'echo' => 1,
|
72
|
+
|
73
|
+
'post_type' => array('post', 'page'),
|
74
|
+
|
75
|
+
);
|
76
|
+
|
77
|
+
my_wp_get_archives( $args )
|
78
|
+
|
79
|
+
?>
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
####functions.phpに記載
|
84
|
+
|
85
|
+
```PHP
|
86
|
+
|
87
|
+
function my_query_vars( $vars ){
|
88
|
+
|
89
|
+
$vars[] = "apt";
|
90
|
+
|
91
|
+
return $vars;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
add_filter( 'query_vars', 'my_query_vars' );
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
function my_pre_get_posts($query) {
|
100
|
+
|
101
|
+
if ( !is_admin() && $query->is_main_query() ) {
|
102
|
+
|
103
|
+
if ($query->is_archive) {
|
104
|
+
|
105
|
+
$apt = $query->get('apt');
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
if( !empty($apt) ) {
|
110
|
+
|
111
|
+
$post_type_array = explode(',', $apt);
|
112
|
+
|
113
|
+
$query->set( 'post_type', $post_type_array );
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
add_action( 'pre_get_posts','my_pre_get_posts' );
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
function my_wp_get_archives( $args = '' ) {
|
128
|
+
|
129
|
+
global $wpdb, $wp_locale;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
$defaults = array(
|
134
|
+
|
135
|
+
'type' => 'monthly', 'limit' => '',
|
136
|
+
|
137
|
+
'format' => 'html', 'before' => '',
|
138
|
+
|
139
|
+
'after' => '', 'show_post_count' => false,
|
140
|
+
|
141
|
+
'echo' => 1, 'order' => 'DESC',
|
142
|
+
|
143
|
+
'post_type' => 'post'
|
144
|
+
|
145
|
+
);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
$r = wp_parse_args( $args, $defaults );
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if( !is_array($r['post_type']) ) {
|
154
|
+
|
155
|
+
$r['post_type'] = array($r['post_type']);
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
foreach( $r['post_type'] as $post_type ) {
|
162
|
+
|
163
|
+
$post_type_object = get_post_type_object( $post_type );
|
164
|
+
|
165
|
+
if ( ! is_post_type_viewable( $post_type_object ) ) {
|
166
|
+
|
167
|
+
return;
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
$post_types[] = $post_type_object->name;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
$r['post_type'] = $post_types;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
if ( '' == $r['type'] ) {
|
182
|
+
|
183
|
+
$r['type'] = 'monthly';
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
if ( ! empty( $r['limit'] ) ) {
|
190
|
+
|
191
|
+
$r['limit'] = absint( $r['limit'] );
|
192
|
+
|
193
|
+
$r['limit'] = ' LIMIT ' . $r['limit'];
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
$order = strtoupper( $r['order'] );
|
200
|
+
|
201
|
+
if ( $order !== 'ASC' ) {
|
202
|
+
|
203
|
+
$order = 'DESC';
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
// this is what will separate dates on weekly archive links
|
210
|
+
|
211
|
+
$archive_week_separator = '–';
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
$sql_where = $wpdb->prepare("WHERE (post_type = %s", $r['post_type'][0]);
|
216
|
+
|
217
|
+
if( !(count($r['post_type'])==1) ) {
|
218
|
+
|
219
|
+
for($i=1; $i<count($r['post_type']); $i++) {
|
220
|
+
|
221
|
+
$sql_where .= $wpdb->prepare(" OR post_type = %s", $r['post_type'][$i]);
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
$sql_where .= ") ";
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
$sql_where .= "AND post_status = 'publish'";
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
$where = $sql_where;
|
236
|
+
|
237
|
+
$join = '';
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
$output = '';
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
$last_changed = wp_cache_get_last_changed( 'posts' );
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
$limit = $r['limit'];
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
// 文字数制限に引っかかったので、続きは別回答に記載
|
254
|
+
|
255
|
+
```
|