Wordpressでカスタム投稿タイプのみ「.html」をつけた場合、管理画面でのパーマリンクの編集ボタンが表示されない。
パーマリンクの設定を
http://ドメイン名/%postname%/
と設定しています。
そこにカスタム投稿タイプのみ最後が.htmlになるように下記のような設定をしています。
php
1<?php 2add_action('rewrite_rules_array', 'rewrite_rules'); 3function rewrite_rules($rules) { 4 $new_rules = array(); 5 $new_rules['[カスタム投稿タイプ名]/(.+?).html$'] = 'index.php?post_type=use&name=$matches[1]'; 6 return $new_rules + $rules; 7} 8 9add_filter('post_type_link', 'custom_post_permalink',10,2); 10add_filter('post_link', 'custom_post_permalink',10,2); 11// for cpt post_type_link (rather than post_link) 12function custom_post_permalink ($link,$post) { 13 global $dump; 14 $dump = $link; 15 if($post->post_type === [カスタム投稿タイプ名]) { 16 return home_url() . '/' . $post->post_type . '/' . $post->post_name . '.html'; 17 } else { 18 return $link; 19 } 20} 21 22add_filter('redirect_canonical', 'remove_redirect_canonical',10,2); 23function remove_redirect_canonical($redirect_url,$requested_url) { 24 if(is_singular([カスタム投稿タイプ名]))) { 25 return $requested_url; 26 } else { 27 return $redirect_url; 28 } 29} 30 31?>
参考
https://wordpress.stackexchange.com/questions/59024/add-html-dot-html-extension-to-custom-post-types
しかし、この状態では管理画面でパーマリンクの右の「編集」ボタンが表示されなくなります。
スラッグ入力欄にスラッグ名を入れればOKなので、問題なくパーマリンクの設定などはできます。
パーマリンクの設定で%postname%を入れると編集ボタンが表示されるので単純に下記のように変更しましたら、編集ボタンは表示されました。
php
1 //return home_url() . '/' . $post->post_type . '/' . $post->post_name . '.html'; 2//↓変更 3return home_url() . '/' . $post->post_type . '/' . '%postname%' . '.html';
しかし今度はパーマリンクの箇所をクリックするとURLが
http://ドメンン名/カスタム投稿タイプ名/%postname%.html
となりbad requestになってしまいます。
よろしくお願いいたします。
あなたの回答
tips
プレビュー