質問編集履歴
6
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,164 +12,168 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
**通常投稿
|
15
|
+
**現在の通常投稿**
|
16
|
+
|
17
|
+
https://hoge.com/news/
|
18
|
+
|
19
|
+
https://hoge.com/news/category/topics/
|
20
|
+
|
21
|
+
https://hoge.com/news/category/media/
|
22
|
+
|
23
|
+
https://hoge.com/news/category/event/
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
**現在のカスタム投稿**
|
28
|
+
|
29
|
+
https://hoge.com/member/
|
30
|
+
|
31
|
+
https://hoge.com/member/member_cat/web/
|
32
|
+
|
33
|
+
https://hoge.com/member/member_cat/media/
|
34
|
+
|
35
|
+
https://hoge.com/member/member_cat/movie/
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
```php
|
44
|
+
|
45
|
+
// カスタム投稿の設定
|
46
|
+
|
47
|
+
function create_post_type() {
|
48
|
+
|
49
|
+
register_post_type(
|
50
|
+
|
51
|
+
'member',
|
52
|
+
|
53
|
+
array(
|
54
|
+
|
55
|
+
'label' => 'MEMBER',
|
56
|
+
|
57
|
+
'labels' => array(
|
58
|
+
|
59
|
+
'all_items' => 'MEMBER投稿',
|
60
|
+
|
61
|
+
),
|
62
|
+
|
63
|
+
'description' => 'MEMBER投稿です',
|
64
|
+
|
65
|
+
'public' => true,
|
66
|
+
|
67
|
+
'menu_position' => 6,
|
68
|
+
|
69
|
+
'has_archive' => true,
|
70
|
+
|
71
|
+
'show_in_rest' => true,
|
72
|
+
|
73
|
+
'rewrite' => array('with_front' => false),
|
74
|
+
|
75
|
+
'supports' => array(
|
76
|
+
|
77
|
+
'title',
|
78
|
+
|
79
|
+
'editor',
|
80
|
+
|
81
|
+
'author',
|
82
|
+
|
83
|
+
'custom-fields',
|
84
|
+
|
85
|
+
'thumbnail',
|
86
|
+
|
87
|
+
),
|
88
|
+
|
89
|
+
)
|
90
|
+
|
91
|
+
);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
add_action( 'init', 'create_post_type' );
|
96
|
+
|
97
|
+
add_theme_support( 'post-thumbnails' );
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
```php
|
104
|
+
|
105
|
+
// タクソノミーの設定
|
106
|
+
|
107
|
+
function add_taxonomy() {
|
108
|
+
|
109
|
+
register_taxonomy(
|
110
|
+
|
111
|
+
'member_cat',
|
112
|
+
|
113
|
+
'member',
|
114
|
+
|
115
|
+
array(
|
116
|
+
|
117
|
+
'label' => 'カテゴリー',
|
118
|
+
|
119
|
+
'singular_label' => 'カテゴリー',
|
120
|
+
|
121
|
+
'labels' => array(
|
122
|
+
|
123
|
+
'all_items' => 'カテゴリー一覧',
|
124
|
+
|
125
|
+
'add_new_item' => '新規カテゴリーを追加'
|
126
|
+
|
127
|
+
),
|
128
|
+
|
129
|
+
'public' => true,
|
130
|
+
|
131
|
+
'show_ui' => true,
|
132
|
+
|
133
|
+
'show_in_nav_menus' => true,
|
134
|
+
|
135
|
+
'hierarchical' => true
|
136
|
+
|
137
|
+
)
|
138
|
+
|
139
|
+
);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
add_rewrite_rule('member/([^/]+)/?$', 'index.php?member_cat=$matches[1]', 'top');
|
144
|
+
|
145
|
+
add_action( 'init', 'add_taxonomy' );
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
### やりたい事
|
152
|
+
|
153
|
+
両方ともURLからcategory(taxonomy)を削除したく
|
154
|
+
|
155
|
+
下記のコードを記載しました。
|
156
|
+
|
157
|
+
(知識があまりなく、コピペで色々試して繋ぎ合わせています)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
**例:**
|
162
|
+
|
163
|
+
https://hoge.com/news/category/topics/
|
164
|
+
|
165
|
+
↓
|
16
166
|
|
17
167
|
https://hoge.com/news/topics/
|
18
168
|
|
169
|
+
|
170
|
+
|
19
|
-
https://hoge.com/
|
171
|
+
https://hoge.com/member/member_cat/topics/
|
20
|
-
|
21
|
-
|
172
|
+
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
173
|
+
↓
|
26
174
|
|
27
175
|
https://hoge.com/member/web/
|
28
176
|
|
29
|
-
https://hoge.com/member/media/
|
30
|
-
|
31
|
-
https://hoge.com/member/movie/
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
```php
|
40
|
-
|
41
|
-
// カスタム投稿の設定
|
42
|
-
|
43
|
-
function create_post_type() {
|
44
|
-
|
45
|
-
register_post_type(
|
46
|
-
|
47
|
-
'member',
|
48
|
-
|
49
|
-
array(
|
50
|
-
|
51
|
-
'label' => 'MEMBER',
|
52
|
-
|
53
|
-
'labels' => array(
|
54
|
-
|
55
|
-
'all_items' => 'MEMBER投稿',
|
56
|
-
|
57
|
-
),
|
58
|
-
|
59
|
-
'description' => 'MEMBER投稿です',
|
60
|
-
|
61
|
-
'public' => true,
|
62
|
-
|
63
|
-
'menu_position' => 6,
|
64
|
-
|
65
|
-
'has_archive' => true,
|
66
|
-
|
67
|
-
'show_in_rest' => true,
|
68
|
-
|
69
|
-
'rewrite' => array('with_front' => false),
|
70
|
-
|
71
|
-
'supports' => array(
|
72
|
-
|
73
|
-
'title',
|
74
|
-
|
75
|
-
'editor',
|
76
|
-
|
77
|
-
'author',
|
78
|
-
|
79
|
-
'custom-fields',
|
80
|
-
|
81
|
-
'thumbnail',
|
82
|
-
|
83
|
-
),
|
84
|
-
|
85
|
-
)
|
86
|
-
|
87
|
-
);
|
88
|
-
|
89
|
-
}
|
90
|
-
|
91
|
-
add_action( 'init', 'create_post_type' );
|
92
|
-
|
93
|
-
add_theme_support( 'post-thumbnails' );
|
94
|
-
|
95
|
-
```
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
```php
|
100
|
-
|
101
|
-
// タクソノミーの設定
|
102
|
-
|
103
|
-
function add_taxonomy() {
|
104
|
-
|
105
|
-
register_taxonomy(
|
106
|
-
|
107
|
-
'member_cat',
|
108
|
-
|
109
|
-
'member',
|
110
|
-
|
111
|
-
array(
|
112
|
-
|
113
|
-
'label' => 'カテゴリー',
|
114
|
-
|
115
|
-
'singular_label' => 'カテゴリー',
|
116
|
-
|
117
|
-
'labels' => array(
|
118
|
-
|
119
|
-
'all_items' => 'カテゴリー一覧',
|
120
|
-
|
121
|
-
'add_new_item' => '新規カテゴリーを追加'
|
122
|
-
|
123
|
-
),
|
124
|
-
|
125
|
-
'public' => true,
|
126
|
-
|
127
|
-
'show_ui' => true,
|
128
|
-
|
129
|
-
'show_in_nav_menus' => true,
|
130
|
-
|
131
|
-
'hierarchical' => true
|
132
|
-
|
133
|
-
)
|
134
|
-
|
135
|
-
);
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
add_rewrite_rule('member/([^/]+)/?$', 'index.php?member_cat=$matches[1]', 'top');
|
140
|
-
|
141
|
-
add_action( 'init', 'add_taxonomy' );
|
142
|
-
|
143
|
-
```
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
### やりたい事
|
148
|
-
|
149
|
-
両方ともURLからcategory(taxonomy)を削除したく
|
150
|
-
|
151
|
-
下記のコードを記載しました。
|
152
|
-
|
153
|
-
(知識があまりなく、コピペで色々試して繋ぎ合わせています)
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
**例:**
|
158
|
-
|
159
|
-
https://hoge.com/news/category/topics/
|
160
|
-
|
161
|
-
↓
|
162
|
-
|
163
|
-
https://hoge.com/news/topics/
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
https://hoge.com/member/member_cat/topics/
|
168
|
-
|
169
|
-
↓
|
170
|
-
|
171
|
-
https://hoge.com/member/web/
|
172
|
-
|
173
177
|
|
174
178
|
|
175
179
|
```php
|
5
文法の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -198,15 +198,23 @@
|
|
198
198
|
|
199
199
|
// URLから/category/を削除する
|
200
200
|
|
201
|
-
//※これ
|
201
|
+
//※これを追記した場合、通常投稿のページャー(一覧)・カスタム投稿のページャーは上手くいきましたが
|
202
|
+
|
203
|
+
通常投稿のカテゴリのページャーが404になりました
|
204
|
+
|
205
|
+
add_filter('user_trailingslashit', 'remcat_function');
|
206
|
+
|
207
|
+
add_filter('init','flushRules');
|
202
208
|
|
203
209
|
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
|
204
210
|
|
211
|
+
|
212
|
+
|
213
|
+
function remcat_function($link) {
|
214
|
+
|
205
|
-
|
215
|
+
return str_replace("/category/", "/", $link);
|
206
|
-
|
216
|
+
|
207
|
-
|
217
|
+
}
|
208
|
-
|
209
|
-
|
210
218
|
|
211
219
|
function flushRules() {
|
212
220
|
|
@@ -226,46 +234,6 @@
|
|
226
234
|
|
227
235
|
}
|
228
236
|
|
229
|
-
function wp_insertMyRewriteQueryVars($vars) {
|
230
|
-
|
231
|
-
array_push($vars, 'id');
|
232
|
-
|
233
|
-
return $vars;
|
234
|
-
|
235
|
-
}
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
add_filter('user_trailingslashit', 'remcat_function');
|
242
|
-
|
243
|
-
add_action('init', 'remcat_flush_rules');
|
244
|
-
|
245
|
-
add_filter('generate_rewrite_rules', 'remcat_rewrite');
|
246
|
-
|
247
|
-
function remcat_function($link) {
|
248
|
-
|
249
|
-
return str_replace("/category/", "/", $link);
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
function remcat_flush_rules() {
|
254
|
-
|
255
|
-
global $wp_rewrite;
|
256
|
-
|
257
|
-
$wp_rewrite->flush_rules();
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
function remcat_rewrite($wp_rewrite) {
|
262
|
-
|
263
|
-
$new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
|
264
|
-
|
265
|
-
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
237
|
|
270
238
|
|
271
239
|
```
|
4
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -198,18 +198,58 @@
|
|
198
198
|
|
199
199
|
// URLから/category/を削除する
|
200
200
|
|
201
|
-
//※これ
|
201
|
+
//※これのみ記載した場合、通常投稿のページャーは上手くいきました
|
202
|
+
|
203
|
+
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
|
204
|
+
|
205
|
+
add_filter('query_vars','wp_insertMyRewriteQueryVars');
|
206
|
+
|
207
|
+
add_filter('init','flushRules');
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
function flushRules() {
|
212
|
+
|
213
|
+
global $wp_rewrite;
|
214
|
+
|
215
|
+
$wp_rewrite->flush_rules();
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
function wp_insertMyRewriteRules($rules) {
|
220
|
+
|
221
|
+
$newrules = array();
|
222
|
+
|
223
|
+
$newrules['(news)/page/?([0-9]{1,})/?$'] = 'index.php?pagename=$matches[1]&paged=$matches[2]';
|
224
|
+
|
225
|
+
return $newrules + $rules;
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
function wp_insertMyRewriteQueryVars($vars) {
|
230
|
+
|
231
|
+
array_push($vars, 'id');
|
232
|
+
|
233
|
+
return $vars;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
202
240
|
|
203
241
|
add_filter('user_trailingslashit', 'remcat_function');
|
204
242
|
|
243
|
+
add_action('init', 'remcat_flush_rules');
|
244
|
+
|
245
|
+
add_filter('generate_rewrite_rules', 'remcat_rewrite');
|
246
|
+
|
205
247
|
function remcat_function($link) {
|
206
248
|
|
207
249
|
return str_replace("/category/", "/", $link);
|
208
250
|
|
209
251
|
}
|
210
252
|
|
211
|
-
add_action('init', 'remcat_flush_rules');
|
212
|
-
|
213
253
|
function remcat_flush_rules() {
|
214
254
|
|
215
255
|
global $wp_rewrite;
|
@@ -218,18 +258,16 @@
|
|
218
258
|
|
219
259
|
}
|
220
260
|
|
221
|
-
|
222
|
-
|
223
|
-
add_filter('generate_rewrite_rules', 'remcat_rewrite');
|
224
|
-
|
225
261
|
function remcat_rewrite($wp_rewrite) {
|
226
262
|
|
227
|
-
|
263
|
+
$new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
|
228
264
|
|
229
265
|
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
|
230
266
|
|
231
267
|
}
|
232
268
|
|
269
|
+
|
270
|
+
|
233
271
|
```
|
234
272
|
|
235
273
|
|
3
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -174,8 +174,32 @@
|
|
174
174
|
|
175
175
|
```php
|
176
176
|
|
177
|
+
//カスタム投稿パーマリンク「/taxonomy/」削除
|
178
|
+
|
179
|
+
//※これのみ記載した場合、カスタム投稿のページャーは上手くいきました
|
180
|
+
|
181
|
+
function my_custom_post_type_permalinks_set($termlink, $term, $taxonomy){
|
182
|
+
|
183
|
+
return str_replace('/'.$taxonomy.'/', '/', $termlink);
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
add_filter('term_link', 'my_custom_post_type_permalinks_set',11,3);
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
add_rewrite_rule( 'member/([^/]+)/?$', 'index.php?member_cat=$matches[1]', 'top' );
|
192
|
+
|
193
|
+
add_rewrite_rule( 'member/([^/]+)/page/([0-9]+)/?$', 'index.php?member_cat=$matches[1]&paged=$matches[2]', 'top' );
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
```php
|
198
|
+
|
177
199
|
// URLから/category/を削除する
|
178
200
|
|
201
|
+
//※これを入れると全てが上手くいかなくなる
|
202
|
+
|
179
203
|
add_filter('user_trailingslashit', 'remcat_function');
|
180
204
|
|
181
205
|
function remcat_function($link) {
|
@@ -210,6 +234,8 @@
|
|
210
234
|
|
211
235
|
|
212
236
|
|
237
|
+
|
238
|
+
|
213
239
|
### 問題
|
214
240
|
|
215
241
|
通常投稿・カスタム投稿両方とも
|
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,21 +14,21 @@
|
|
14
14
|
|
15
15
|
**通常投稿(一部)**
|
16
16
|
|
17
|
-
https://hoge/news/topics/
|
17
|
+
https://hoge.com/news/topics/
|
18
|
-
|
18
|
+
|
19
|
-
https://hoge/news/media/
|
19
|
+
https://hoge.com/news/media/
|
20
|
-
|
20
|
+
|
21
|
-
https://hoge/news/event/
|
21
|
+
https://hoge.com/news/event/
|
22
22
|
|
23
23
|
|
24
24
|
|
25
25
|
**カスタム投稿(一部)**
|
26
26
|
|
27
|
-
https://hoge/member/web/
|
27
|
+
https://hoge.com/member/web/
|
28
|
-
|
28
|
+
|
29
|
-
https://hoge/member/media/
|
29
|
+
https://hoge.com/member/media/
|
30
|
-
|
30
|
+
|
31
|
-
https://hoge/member/movie/
|
31
|
+
https://hoge.com/member/movie/
|
32
32
|
|
33
33
|
|
34
34
|
|
@@ -146,7 +146,7 @@
|
|
146
146
|
|
147
147
|
### やりたい事
|
148
148
|
|
149
|
-
両方ともURLからcategoryを削除したく
|
149
|
+
両方ともURLからcategory(taxonomy)を削除したく
|
150
150
|
|
151
151
|
下記のコードを記載しました。
|
152
152
|
|
@@ -156,11 +156,19 @@
|
|
156
156
|
|
157
157
|
**例:**
|
158
158
|
|
159
|
-
https://hoge/news/category/topics/
|
159
|
+
https://hoge.com/news/category/topics/
|
160
160
|
|
161
161
|
↓
|
162
162
|
|
163
|
-
https://hoge/news/topics/
|
163
|
+
https://hoge.com/news/topics/
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
https://hoge.com/member/member_cat/topics/
|
168
|
+
|
169
|
+
↓
|
170
|
+
|
171
|
+
https://hoge.com/member/web/
|
164
172
|
|
165
173
|
|
166
174
|
|
@@ -212,6 +220,26 @@
|
|
212
220
|
|
213
221
|
|
214
222
|
|
223
|
+
### やったこと
|
224
|
+
|
225
|
+
そもそもコピペで繋ぎ合わせたコードなので、上記に記載した
|
226
|
+
|
227
|
+
**// URLから/category/を削除する**
|
228
|
+
|
229
|
+
を削除して動くか確認しました。
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
https://hoge.com/news/category/topics/page/2/
|
234
|
+
|
235
|
+
https://hoge.com/news/member_cat/web/page/2/
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
category(taxonomy)付きでは問題なく動きました。
|
240
|
+
|
241
|
+
|
242
|
+
|
215
243
|
### 質問
|
216
244
|
|
217
245
|
どのように修正して、どのようなコードを追加すれば良いのか
|
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -136,7 +136,7 @@
|
|
136
136
|
|
137
137
|
}
|
138
138
|
|
139
|
-
add_rewrite_rule('member/([^/]+)/?$', 'index.php?
|
139
|
+
add_rewrite_rule('member/([^/]+)/?$', 'index.php?member_cat=$matches[1]', 'top');
|
140
140
|
|
141
141
|
add_action( 'init', 'add_taxonomy' );
|
142
142
|
|