カスタム投稿タイプを作成し、新規記事を作成しようとするとスラッグが、日本語タイトルになったりします。ユーザーもスラッグを意識しないので日本語のまま、なんて事が多いです。
WordPressで、自動化してほしい
ということで。
目次
functions.php に書き込み
add_filter( 'post_type_link', 'custom_post_type_link', 1, 2 );
function custom_post_type_link( $link, $post ){
if ( '[name]' === $post->post_type ) {
return home_url( '/archives/[name]/' . $post->ID );
}
return $link;
}
add_filter( 'rewrite_rules_array', 'custom_rewrite_rules_array' );
function custom_rewrite_rules_array( $rules ) {
$new_rules = array(
'archives/[name]/([0-9]+)/?$' => 'index.php?post_type=[name]&p=$matches[1]',
);
return $new_rules + $rules;
}
カスタム投稿タイプの作成
ちなみにカスタム投稿タイプの作成も面倒なのでプラグインで解決しちゃっています。
WordPress.org 日本語


Custom Post Type UI
Admin UI for creating custom content types like post types and taxonomies
コメント