WordPressでカスタム投稿タイプを使っている方であれば、タクソノミー(分類)にターム(カテゴリー)を登録して使うケースが多いと思います。
今回は、ターム(カテゴリー)一覧ページのパーマリンクを「/カスタム投稿タイプ名/ターム名」のようなシンプルな形式にカスタマイズする方法をご紹介します。

■WPのバージョンとパーマリンク設定
バージョン | WordPress 4.8 |
カスタム投稿タイプ | news |
タクソノミー | news-category |
ターム | event |
パーマリンク設定 | /%category%/%postname%/ |
作業の流れ
- STEP1 カスタム投稿タイプを設定する。
- STEP2 カスタム投稿でタクソノミー(分類)を設定する。
- STEP3 独自パーマリンクのルールを追加する。
- STEP4 ターム(カテゴリー)を登録する。
- STEP5 パーマリンクを設定する。
STEP1 カスタム投稿タイプを設定する
お使いのテーマから function.php を開き、以下のコードを追記します。
1 2 3 4 5 6 7 8 9 10 |
<?php /* カスタム投稿タイプの登録 */ $news_args = array( 'labels' => array( 'name' => 'ニュース' ) , 'public' => true, 'has_archive' => true, 'rewrite' => array( 'slug' => 'news'), ); register_post_type( apply_filters( 'news_post_type', 'news' ), apply_filters( 'news_post_type_args', $news_args ) ); ?> |
STEP2 カスタム投稿でタクソノミー(分類)を設定する
STEP1で開いた function.php に、以下のコードを追記します。
1 2 3 4 5 6 7 8 9 10 11 |
<?php /* カスタム投稿タイプのタクソノミー登録 */ add_action( 'init', 'news_taxonomies', 0 ); function news_taxonomies() { register_taxonomy( 'news-category', array("news"), array( 'hierarchical' => true, 'label' => 'カテゴリー', 'query_var' => true, 'rewrite' => true ) ); } ?> |
STEP3 独自パーマリンクのルールを追加する
STEP1~2の記述をした function.php に、以下のコードを追記します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php /* パーマリンクを拡張 */ add_action( 'generate_rewrite_rules', 'my_rewrite' ); function my_rewrite( $wp_rewrite ){ $taxonomies = get_taxonomies(); $taxonomies = array_slice($taxonomies,4,count($taxonomies)-1); foreach ( $taxonomies as $taxonomy ) : $args['taxonomy'] = $taxonomy; $args['hide_empty'] = false; $cats = get_categories( $args ); foreach ( $cats as $k => $v ){ $new_rules['news/'.$taxonomy.'/'.$v->category_nicename.'/page/([0-9]{1,})/?$'] = 'index.php?post_type=news&taxonomy='.$taxonomy.'&term='.$v->category_nicename.'&paged=$matches[1]'; $new_rules['news/'.$v->category_nicename.'/?$'] = 'index.php?post_type=news&taxonomy='.$taxonomy.'&term='.$v->category_nicename; $new_rules['news/'.$v->category_nicename.'/page/([0-9]{1,})/?$'] = 'index.php?post_type=news&taxonomy='.$taxonomy.'&term='.$v->category_nicename.'&paged=$matches[1]'; } $post_types = get_taxonomy($taxonomy)->object_type; foreach ($post_types as $post_type){ $new_rules[$post_type.'/'.$taxonomy.'/(.+?)/?$'] = 'index.php?taxonomy='.$taxonomy.'&term='.$wp_rewrite->preg_index(1); } $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules); endforeach; } ?> |
STEP4 ターム(カテゴリー)を登録する
STEP2で作成したタクソノミーに、管理画面から新規タームを登録します。
名前:「イベント情報」、スラッグ:「event」を登録します。

STEP5 パーマリンクを設定する
管理画面 > 設定 > パーマリンク よりカスタム構造を選択し「/%category%/%postname%/」を入力して保存ボタンを押します。

サイドエリアにタームリンクを追加する
sidebar.phpを開き、以下のコードを追記します。
以下はWordPress 4.8で採用されている「twentyseventeen」テーマの場合です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<aside id="secondary" class="widget-area" role="complementary"> <section id="meta-2" class="widget widget_meta"><h2 class="widget-title">タクソノミー一覧</h2> <ul> <?php $terms = get_terms('news-category'); foreach ($terms as $term) { ?> <li><a href="<?=get_term_link($term->slug, 'news-category');?>"><?=$term->name;?></a></li> <?php }?> </ul> </section> <?php dynamic_sidebar( 'sidebar-1' ); ?> </aside><!-- #secondary --> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
add_filter( 'term_link', 'my_term_link' ,10,3); function my_term_link( $termlink, $term, $taxonomy){ $t=get_taxonomy($taxonomy); $wp_home = get_option("home"); $post_type = $t->object_type[0]; if(!isset($t->object_type[1])) { if( $post_type === 'news' ){ $termlink = str_replace( $wp_home , $wp_home."/".$post_type , str_replace( $t->name.'/' , '' , $termlink ) ); } else { $termlink = str_replace( 'category/' , '' , $termlink ); } } return $termlink; } |