回答編集履歴
1
追記(コードを改善)
test
CHANGED
@@ -57,3 +57,73 @@
|
|
57
57
|
}
|
58
58
|
|
59
59
|
```
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
---
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
**追記**
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
`is_preview()`判定を追加しました。
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```PHP
|
76
|
+
|
77
|
+
add_action( 'template_redirect', 'teratail243258_shimosaka_h' );
|
78
|
+
|
79
|
+
function teratail243258_shimosaka_h()
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
global $post;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
$uri = $_SERVER['REQUEST_URI'];
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
if (!is_object($post) || !$post->post_parent || is_preview())
|
92
|
+
|
93
|
+
{
|
94
|
+
|
95
|
+
return;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
else
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
$parent_slug = get_post($post->post_parent)->post_name;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
// URIに親投稿のスラッグが含まれていなかったら
|
110
|
+
|
111
|
+
if (!stristr($uri, $parent_slug))
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
global $wp_query;
|
116
|
+
|
117
|
+
$wp_query->set_404();
|
118
|
+
|
119
|
+
status_header(404);
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
return;
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
```
|