ブログ

BLOG

CATEGORY

タクソノミースラッグをURLから取り除く

タクソノミースラッグを取り除く

タクソノミースラッグをパーマリンクから取り除く方法がわかったのでメモ

WordPress側の設定

  • Custom Post Type Permalinks をインストール
  • カスタム投稿タイプのパーマリンク設定を /%タクソノミー名%/%post_id%/ に変更
  • カスタマイズされたカスタムタクソノミーのパーマリンクを使用する。にチェックを入れる
  • 設定→パーマリンク設定 で変更を保存を押す

function.phpへの記述

function my_custom_post_type_permalinks_set( $termlink, $term, $taxonomy ) {
return str_replace( '/' . $taxonomy . '/', '/', $termlink );
}
add_filter( 'term_link', 'my_custom_post_type_permalinks_set', 11, 3 );

function my_custom_post_type_permalinks_rule() {
add_rewrite_rule( 'カスタム投稿名/([^/]+)/([^/]+)/page/([0-9]+)/?$', 'index.php?タクソノミー名=$matches[1]/$matches[2]&paged=$matches[3]', 'top' );
add_rewrite_rule( 'カスタム投稿名/([^/]+)/page/([0-9]+)/?$', 'index.php?タクソノミー名=$matches[1]&paged=$matches[2]', 'top' );
add_rewrite_rule( 'カスタム投稿名/([^/]+)/([^/]+)/([0-9]+)/?$', 'index.php?post_type=カスタム投稿名&p=$matches[3]', 'top' );
add_rewrite_rule( 'カスタム投稿名/([^/]+)/([0-9]+)/?$', 'index.php?post_type=カスタム投稿名&p=$matches[2]', 'top' );
add_rewrite_rule( 'カスタム投稿名/([^/]+)/([^/]+)/?$', 'index.php?タクソノミー名=$matches[1]/$matches[2]', 'top' );
add_rewrite_rule( 'カスタム投稿名/([^/]+)/?$', 'index.php?タクソノミー名=$matches[1]', 'top' );
add_action( 'init', 'my_custom_post_type_permalinks_rule' );