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

回答編集履歴

3

仮想ページのリダイレクト処理を想定

2019/07/24 05:25

投稿

YukiYamashina
YukiYamashina

スコア1011

answer CHANGED
@@ -1,8 +1,6 @@
1
1
  `pre_get_posts` を利用して、カテゴリーページかつリクエストの `basename` がスラッグ名となる投稿が存在する場合のみ、投稿ページとしてクエリすることで可能だと思います。
2
2
 
3
3
  ```php
4
- <?php
5
-
6
4
  add_filter( 'pre_get_posts', function( WP_Query $query ) {
7
5
  global $wp;
8
6
 
@@ -10,6 +8,12 @@
10
8
  return $query;
11
9
  }
12
10
 
11
+ // redirect /parent-cat/child-cat/child-cat/ to /parent-cat/child-cat.
12
+ if ( isset( $query->query['name'] ) && isset( $query->query['category_name'] ) && basename( $query->query['category_name'] ) === $query->query['name'] ) {
13
+ wp_redirect( home_url( $query->query['category_name'] ), 301 );
14
+ exit;
15
+ }
16
+
13
17
  $post_name = basename( $wp->request );
14
18
 
15
19
  if ( $query->is_category() && get_page_by_path( $post_name, 'OBJECT', 'post' ) ) {
@@ -18,24 +22,6 @@
18
22
  $query->parse_query( "page={$page}&name={$post_name}&category_name={$category_name}" );
19
23
  }
20
24
 
21
- // redirect /parent-cat/child-cat/child-cat/ to /parent-cat/child-cat.
22
- if ( isset( $query->query['name'] ) && isset( $query->query['category_name'] ) && $wp->request === $query->query['category_name'] . '/' . $query->query['name'] ) {
23
- wp_redirect( home_url( $query->query['category_name'] ), 301 );
24
- exit;
25
- }
26
-
27
25
  return $query;
28
26
  } );
29
-
30
- // Prevent /parent-cat/child-cat/ from redirecting to /parent-cat/child-cat/child-cat/.
31
- add_filter( 'redirect_canonical', function( string $redirect_url, string $requested_url ) {
32
- global $wp, $wp_query;
33
-
34
- if ( isset( $wp_query->query['name'] ) && isset( $wp_query->query['category_name'] ) && $wp->request === $wp_query->query['category_name'] ) {
35
- return $requested_url;
36
- }
37
-
38
- return $redirect_url;
39
- }, 10, 2 );
40
-
41
27
  ```

2

リダイレクト処理追加

2019/07/24 05:25

投稿

YukiYamashina
YukiYamashina

スコア1011

answer CHANGED
@@ -18,7 +18,24 @@
18
18
  $query->parse_query( "page={$page}&name={$post_name}&category_name={$category_name}" );
19
19
  }
20
20
 
21
+ // redirect /parent-cat/child-cat/child-cat/ to /parent-cat/child-cat.
22
+ if ( isset( $query->query['name'] ) && isset( $query->query['category_name'] ) && $wp->request === $query->query['category_name'] . '/' . $query->query['name'] ) {
23
+ wp_redirect( home_url( $query->query['category_name'] ), 301 );
24
+ exit;
25
+ }
26
+
21
27
  return $query;
22
28
  } );
23
29
 
30
+ // Prevent /parent-cat/child-cat/ from redirecting to /parent-cat/child-cat/child-cat/.
31
+ add_filter( 'redirect_canonical', function( string $redirect_url, string $requested_url ) {
32
+ global $wp, $wp_query;
33
+
34
+ if ( isset( $wp_query->query['name'] ) && isset( $wp_query->query['category_name'] ) && $wp->request === $wp_query->query['category_name'] ) {
35
+ return $requested_url;
36
+ }
37
+
38
+ return $redirect_url;
39
+ }, 10, 2 );
40
+
24
41
  ```

1

日本語修正

2019/07/16 15:29

投稿

YukiYamashina
YukiYamashina

スコア1011

answer CHANGED
@@ -1,4 +1,4 @@
1
- `pre_get_posts` を利用して、カテゴリーページかつリクエストの `basename` がスラッグ名となる投稿が存在すれば投稿ページとしてクエリすれば可能だと思います。
1
+ `pre_get_posts` を利用して、カテゴリーページかつリクエストの `basename` がスラッグ名となる投稿が存在する場合のみ、投稿ページとしてクエリすることで可能だと思います。
2
2
 
3
3
  ```php
4
4
  <?php