どうも、個人ビジネス部PHP専門のコウイチです。
今回は、パーマリンクに関して少し突っ込んだ内容を解説しようと思います。
パーマリンクが何か、分からない方は、別の記事で解説しているので、そちらをご覧ください。
↓
【WordPress】最初にやっておくべきパーマリンクの設定方法を解説
[blogcard url="https://businessdepartment.jp/wordpress/permalink/"]
今回の記事は、パーマリンクを以下のように運用したい場合に役立つ内容です。
- 記事ページに対して、ディレクトリのようにカテゴリ名を使ったURLでアクセスさせたい。
(例)https://example.com/[カテゴリ名]/[サブカテゴリ名(あれば)]/[記事タイトル]/
- カテゴリ名自体を後から変更しても、記事のURLは変えたくない。(※ここ重要!)
このようなパーマリンクは、ユーザーにも検索エンジンにも優しいです。
URLを人が見ればそのサイトの構造が把握でき、検索エンジンも、サイト構造を理解しやすくなるからです。
普通にやるなら
このように、パーマリンク設定で「/%category%/%postname%/」をセットすればよいですよね?
その通りです。
そうすれば、先ほど例に出したような
(例)https://example.com/[カテゴリ名]/[サブカテゴリ名(あれば)]/[記事タイトル]/
というURLは実現できます。
しかし、これでは途中でカテゴリ名自体を変えた時に、記事のURLまで変わってしまいます。
これはまずいのです。今までアクセスできていたページにアクセスできなくなるわけで、検索エンジンの記事の評価もリセットされてしまうのですから。
では、カテゴリ名自体を仮に変えたとしても、URLを変えずに済むためためには、記事編集画面の上部のパーマリンク編集エリアで、カテゴリ名を手打ちするしかありません。
そのためには、パーマリンク編集エリアにスラッシュを入力する必要があります。
しかし、デフォルトの状態では、パーマリンク編集エリアにスラッシュを入力することができません。
スラッシュを入力するためには、functions.phpにソースコードを追記しなければならないのです。
この記事では、短いコードでこうしたパーマリンク構造を実現する方法を、ご紹介します。
ググってみても、なかなかドンピシャな記事を見つけることができませんでした。
是非参考にしてください!
パーマリンクにスラッシュ「/」を含めることができない
WordPressでは、記事編集画面の上部でパーマリンクを編集する際、スラッシュ「/」を含めることができません。
このように、スラッシュを入力してOKを押しても、勝手にハイフンに変換されてしまいます(;゚Д゚)
勝手にハイフンに変換されないようにする
functions.phpにコードを追記
sanitize_title_with_dashesというフィルタで、パーマリンクの手動入力時にハイフンに変換しています。
ですので、このフィルターを除去します。
ただし、フィルタを除去するだけでは、パーマリンクの文字列が日本語の場合、URIエンコードされずにDBに保存されます。
そのためページにアクセスした際、URIエンコードされた文字列と、DB上のエンコードされていない文字列が比較され、一致せず、ページがNot Foundになってしまうという現象が起こります。
それを防ぐため、自前でしっかりとURIエンコードフィルタを追加しておきます。
functions.phpに以下のコードを追加しましょう。
1 2 3 4 5 6 7 8 |
// 各記事のページでパーマリンク文字列を入力する際、スラッシュがハイフンに自動変換されてしまうのを防ぐ remove_filter('sanitize_title', 'sanitize_title_with_dashes'); add_filter('sanitize_title', function($title, $raw_title = '', $context = 'display'){ if(seems_utf8($title)){ $title = utf8_uri_encode($title, 200); } return $title; }); |
結果
めでたく、パーマリンクにスラッシュが入力できるようになりました。
しかし、そのままではページが「Not Found」になる
このまま記事ページを表示してみてください。「ページが見つかりません」となってしまうはずです。
なぜなら、スラッシュが含まれるパーマリンク文字列は、既存のパーマリンクのリライトルールにヒットしないからです。
リライトルールとは何か?
WordPressでは、例えば以下のような記事URLがあったとしますよね。
https://example.com/WordPress
このURLにアクセスすると、
https://example.com/index.php?name=WordPress
このようなURLに自動変換されて、ページにアクセスする仕組みとなっています(この変換は一例です)。
このように、URLを書き換えるルールを「リライトルール」と言います。
こうすることによって、あたかも
https://example.com/WordPress
というURLで、記事にアクセスしているかのように見せかけているのです。
パーマリンクのリライトルールは、正規表現文字列をキーとした連想配列で保存されていて、
1 |
<?php var_dump( get_option( 'rewrite_rules' ) ); ?> |
と書くと表示することができます。
表示してみた例がこちら↓
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
array(138) { ["^wp-json/?$"]=> string(22) "index.php?rest_route=/" ["^wp-json/(.*)?"]=> string(33) "index.php?rest_route=/$matches[1]" ["^index.php/wp-json/?$"]=> string(22) "index.php?rest_route=/" ["^index.php/wp-json/(.*)?"]=> string(33) "index.php?rest_route=/$matches[1]" ["^ss/\-/([^/]+)/?([^/]*)/?"]=> string(74) "index.php?step_sales_api_action=$matches[1]&step_sales_api_key=$matches[2]" ["lp/?$"]=> string(22) "index.php?post_type=lp" ["lp/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(39) "index.php?post_type=lp&feed=$matches[1]" ["lp/(feed|rdf|rss|rss2|atom)/?$"]=> string(39) "index.php?post_type=lp&feed=$matches[1]" ["lp/page/([0-9]{1,})/?$"]=> string(40) "index.php?post_type=lp&paged=$matches[1]" ["category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(52) "index.php?category_name=$matches[1]&feed=$matches[2]" ["category/(.+?)/(feed|rdf|rss|rss2|atom)/?$"]=> string(52) "index.php?category_name=$matches[1]&feed=$matches[2]" ["category/(.+?)/embed/?$"]=> string(46) "index.php?category_name=$matches[1]&embed=true" ["category/(.+?)/page/?([0-9]{1,})/?$"]=> string(53) "index.php?category_name=$matches[1]&paged=$matches[2]" ["category/(.+?)/?$"]=> string(35) "index.php?category_name=$matches[1]" ["tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(42) "index.php?tag=$matches[1]&feed=$matches[2]" ["tag/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(42) "index.php?tag=$matches[1]&feed=$matches[2]" ["tag/([^/]+)/embed/?$"]=> string(36) "index.php?tag=$matches[1]&embed=true" ["tag/([^/]+)/page/?([0-9]{1,})/?$"]=> string(43) "index.php?tag=$matches[1]&paged=$matches[2]" ["tag/([^/]+)/?$"]=> string(25) "index.php?tag=$matches[1]" ["type/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(50) "index.php?post_format=$matches[1]&feed=$matches[2]" ["type/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(50) "index.php?post_format=$matches[1]&feed=$matches[2]" ["type/([^/]+)/embed/?$"]=> string(44) "index.php?post_format=$matches[1]&embed=true" ["type/([^/]+)/page/?([0-9]{1,})/?$"]=> string(51) "index.php?post_format=$matches[1]&paged=$matches[2]" ["type/([^/]+)/?$"]=> string(33) "index.php?post_format=$matches[1]" ["ss/.+?/attachment/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["ss/.+?/attachment/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["ss/.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["ss/.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["ss/.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["ss/.+?/attachment/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["ss/(.+?)/embed/?$"]=> string(43) "index.php?step-sales=$matches[1]&embed=true" ["ss/(.+?)/trackback/?$"]=> string(37) "index.php?step-sales=$matches[1]&tb=1" ["ss/(.+?)/page/?([0-9]{1,})/?$"]=> string(50) "index.php?step-sales=$matches[1]&paged=$matches[2]" ["ss/(.+?)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?step-sales=$matches[1]&cpage=$matches[2]" ["ss/(.+?)(?:/([0-9]+))?/?$"]=> string(49) "index.php?step-sales=$matches[1]&page=$matches[2]" ["keni_cc/[^/]+/attachment/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["keni_cc/[^/]+/attachment/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["keni_cc/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["keni_cc/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["keni_cc/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["keni_cc/[^/]+/attachment/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["keni_cc/([^/]+)/embed/?$"]=> string(40) "index.php?keni_cc=$matches[1]&embed=true" ["keni_cc/([^/]+)/trackback/?$"]=> string(34) "index.php?keni_cc=$matches[1]&tb=1" ["keni_cc/([^/]+)/page/?([0-9]{1,})/?$"]=> string(47) "index.php?keni_cc=$matches[1]&paged=$matches[2]" ["keni_cc/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(47) "index.php?keni_cc=$matches[1]&cpage=$matches[2]" ["keni_cc/([^/]+)(?:/([0-9]+))?/?$"]=> string(46) "index.php?keni_cc=$matches[1]&page=$matches[2]" ["keni_cc/[^/]+/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["keni_cc/[^/]+/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["keni_cc/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["keni_cc/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["keni_cc/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["keni_cc/[^/]+/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["lp/[^/]+/attachment/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["lp/[^/]+/attachment/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["lp/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["lp/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["lp/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["lp/[^/]+/attachment/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["lp/([^/]+)/embed/?$"]=> string(35) "index.php?lp=$matches[1]&embed=true" ["lp/([^/]+)/trackback/?$"]=> string(29) "index.php?lp=$matches[1]&tb=1" ["lp/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(41) "index.php?lp=$matches[1]&feed=$matches[2]" ["lp/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(41) "index.php?lp=$matches[1]&feed=$matches[2]" ["lp/([^/]+)/page/?([0-9]{1,})/?$"]=> string(42) "index.php?lp=$matches[1]&paged=$matches[2]" ["lp/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(42) "index.php?lp=$matches[1]&cpage=$matches[2]" ["lp/([^/]+)(?:/([0-9]+))?/?$"]=> string(41) "index.php?lp=$matches[1]&page=$matches[2]" ["lp/[^/]+/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["lp/[^/]+/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["lp/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["lp/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["lp/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["lp/[^/]+/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" [".*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$"]=> string(18) "index.php?feed=old" [".*wp-app\.php(/.*)?$"]=> string(19) "index.php?error=403" [".*wp-register.php$"]=> string(23) "index.php?register=true" ["feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(27) "index.php?&feed=$matches[1]" ["(feed|rdf|rss|rss2|atom)/?$"]=> string(27) "index.php?&feed=$matches[1]" ["embed/?$"]=> string(21) "index.php?&embed=true" ["page/?([0-9]{1,})/?$"]=> string(28) "index.php?&paged=$matches[1]" ["comments/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(42) "index.php?&feed=$matches[1]&withcomments=1" ["comments/(feed|rdf|rss|rss2|atom)/?$"]=> string(42) "index.php?&feed=$matches[1]&withcomments=1" ["comments/embed/?$"]=> string(21) "index.php?&embed=true" ["search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(40) "index.php?s=$matches[1]&feed=$matches[2]" ["search/(.+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(40) "index.php?s=$matches[1]&feed=$matches[2]" ["search/(.+)/embed/?$"]=> string(34) "index.php?s=$matches[1]&embed=true" ["search/(.+)/page/?([0-9]{1,})/?$"]=> string(41) "index.php?s=$matches[1]&paged=$matches[2]" ["search/(.+)/?$"]=> string(23) "index.php?s=$matches[1]" ["author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(50) "index.php?author_name=$matches[1]&feed=$matches[2]" ["author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(50) "index.php?author_name=$matches[1]&feed=$matches[2]" ["author/([^/]+)/embed/?$"]=> string(44) "index.php?author_name=$matches[1]&embed=true" ["author/([^/]+)/page/?([0-9]{1,})/?$"]=> string(51) "index.php?author_name=$matches[1]&paged=$matches[2]" ["author/([^/]+)/?$"]=> string(33) "index.php?author_name=$matches[1]" ["([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(80) "index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]" ["([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=> string(80) "index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]" ["([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/embed/?$"]=> string(74) "index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&embed=true" ["([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$"]=> string(81) "index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]" ["([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$"]=> string(63) "index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]" ["([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(64) "index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]" ["([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=> string(64) "index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]" ["([0-9]{4})/([0-9]{1,2})/embed/?$"]=> string(58) "index.php?year=$matches[1]&monthnum=$matches[2]&embed=true" ["([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$"]=> string(65) "index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]" ["([0-9]{4})/([0-9]{1,2})/?$"]=> string(47) "index.php?year=$matches[1]&monthnum=$matches[2]" ["([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(43) "index.php?year=$matches[1]&feed=$matches[2]" ["([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$"]=> string(43) "index.php?year=$matches[1]&feed=$matches[2]" ["([0-9]{4})/embed/?$"]=> string(37) "index.php?year=$matches[1]&embed=true" ["([0-9]{4})/page/?([0-9]{1,})/?$"]=> string(44) "index.php?year=$matches[1]&paged=$matches[2]" ["([0-9]{4})/?$"]=> string(26) "index.php?year=$matches[1]" [".?.+?/attachment/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" [".?.+?/attachment/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" [".?.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" [".?.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" [".?.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" [".?.+?/attachment/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["(.?.+?)/embed/?$"]=> string(41) "index.php?pagename=$matches[1]&embed=true" ["(.?.+?)/trackback/?$"]=> string(35) "index.php?pagename=$matches[1]&tb=1" ["(.?.+?)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(47) "index.php?pagename=$matches[1]&feed=$matches[2]" ["(.?.+?)/(feed|rdf|rss|rss2|atom)/?$"]=> string(47) "index.php?pagename=$matches[1]&feed=$matches[2]" ["(.?.+?)/page/?([0-9]{1,})/?$"]=> string(48) "index.php?pagename=$matches[1]&paged=$matches[2]" ["(.?.+?)/comment-page-([0-9]{1,})/?$"]=> string(48) "index.php?pagename=$matches[1]&cpage=$matches[2]" ["(.?.+?)(?:/([0-9]+))?/?$"]=> string(47) "index.php?pagename=$matches[1]&page=$matches[2]" ["[^/]+/attachment/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["[^/]+/attachment/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["[^/]+/attachment/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" ["([^/]+)/embed/?$"]=> string(37) "index.php?name=$matches[1]&embed=true" ["([^/]+)/trackback/?$"]=> string(31) "index.php?name=$matches[1]&tb=1" ["([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(43) "index.php?name=$matches[1]&feed=$matches[2]" ["([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(43) "index.php?name=$matches[1]&feed=$matches[2]" ["([^/]+)/page/?([0-9]{1,})/?$"]=> string(44) "index.php?name=$matches[1]&paged=$matches[2]" ["([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(44) "index.php?name=$matches[1]&cpage=$matches[2]" ["([^/]+)(?:/([0-9]+))?/?$"]=> string(43) "index.php?name=$matches[1]&page=$matches[2]" ["[^/]+/([^/]+)/?$"]=> string(32) "index.php?attachment=$matches[1]" ["[^/]+/([^/]+)/trackback/?$"]=> string(37) "index.php?attachment=$matches[1]&tb=1" ["[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$"]=> string(49) "index.php?attachment=$matches[1]&feed=$matches[2]" ["[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$"]=> string(50) "index.php?attachment=$matches[1]&cpage=$matches[2]" ["[^/]+/([^/]+)/embed/?$"]=> string(43) "index.php?attachment=$matches[1]&embed=true" } |
かなり多くのルールが登録されていることが分かります。
上にあるルールほど優先されて、URLが置換される仕組みです。
で、この、パーマリンクの既存のリライトルールには、スラッシュを含まないパーマリンクにヒットするリライトルールが存在しています。
["([^/]+)(?:/([0-9]+))?/?$"] => "index.php?name=$matches[1]&page=$matches[2]"これです。黄色の部分が、スラッシュを含まない文字列を表しています。
これを、スラッシュを含むことを許すルールに書き換えます。
["^(.+)(?:/([0-9]+))?/?$"] ⇒ "index.php?name=$matches[1]&page=$matches[2]"このように書き換えてしまえば問題なさそうです。
functions.phpにコードを追記
functions.phpに、以下のコードを追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/* リライトルールを書き換える。*/ function my_rewrite_rules_array($my_rewrite) { $return_rule = array(); foreach ($my_rewrite as $regex => $rewrite){ // スラッシュをはじかないルールに書き換える。 $regex = str_replace('[^/]', '.', $regex); // リライトルールをセット $return_rule[$regex] = $rewrite; } return $return_rule; } add_filter('rewrite_rules_array', 'my_rewrite_rules_array'); |
post_rewrite_rulesフィルターは、パーマリンクを生成するリライトルールのフィルターです。
コールバック関数の引数からリライトルールの連想配列を受け取ることができるので、foreach文の中でリライトルールを書き換えています。
スラッシュをはじいているパターンをすべて書き換えているので、思わぬ動作をする可能性もあります。ご自身でページがちゃんと表示されるか、確認してくださいね。
これで、しっかりと記事のページが表示されるはずです。
読ませていただきました。私これ関係の知識が浅いのですが質問させてください。親テーマが更新された場合、URLに影響が出る可能性は考えられますか?
コメントありがとうございます。
親テーマを変更すればURLはデフォルトの設定で表示されますよ。
リライトで手こずっていました。おかげ様で助かりました!
リライトで手こずっていたので、こちらの記事たいへん参考になりました。ありがとうございます!