カスタム投稿タイプでは、パーマリンク設定で「%post_id%」を指定しても、
通常は反映されませんが、以下のコードを functions.php に書く事で反映されるようになります。
以下のコードは、「/ カスタム投稿タイプ / 投稿ID」とする場合です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_action('init', 'myposttype_rewrite'); function myposttype_rewrite() { global $wp_rewrite; $queryarg = 'post_type=カスタム投稿タイプ&p='; $wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)', $queryarg); $wp_rewrite->add_permastruct('カスタム投稿タイプ', '/カスタム投稿タイプ/%post_id%', false); } add_filter('post_type_link', 'myposttype_permalink', 1, 3); function myposttype_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('カスタム投稿タイプ'); $newlink = str_replace("%post_id%", $post->ID, $newlink); $newlink = home_url(user_trailingslashit($newlink)); return $newlink; } |
functions.php を書き換えた後に、
必ず「パーマリンク設定」で「変更を保存」してください。
「変更を保存」しないと 404 エラーがでます。
【補足】
ソースの一部を修正しました[2013/12/3]