
カスタム投稿タイプのパーマーリンクの最後に.htmlの拡張子をつけなくてはならなくなった。
今回は/customer/%customer_id%.html
例) /customer/0001.html 数字表示になる
add_action('init', 'myposttype_customer_rewrite');
function myposttype_customer_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=customer&p=';
$wp_rewrite->use_trailing_slashes=false;//最後のスラッシュ無し
$wp_rewrite->add_rewrite_tag('%customer_id%', '([^/]+)',$queryarg);
$wp_rewrite->add_permastruct('customer', '/customer/%customer_id%.html', false);
}
add_filter('post_type_link', 'myposttype_customer_permalink', 1, 3);
function myposttype_customer_permalink($post_link, $id = 0, $leavename) {
global $wp_rewrite;
$post = get_post($id);
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
$newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
$newlink = home_url(user_trailingslashit($newlink));
return $newlink;
}
パーマリンクを変更後、404エラーになってしまう場合
管理画面より
設定 > パーマーリンク設定 > 設定を保存
をして再設定する