###前提・実現したいこと
カスタム投稿タイプにおいてスラッグ表記を一種類はID名にもう一種類をページ名にしたいです。
まずカスタム投稿タイプの設定をfunctions.phpに記述しました。
作りたいカスタム投稿名
①ニュース /news/
②実績 /coordination/
php
1//ニュース 2add_action('init', 'add_news_post_type'); 3function add_news_post_type() { 4 $params = array( 5 'labels' => array( 6 'name' => 'ニュース', 7 'singular_name' => 'ニュース記事', 8 'add_new' => '新規追加', 9 'add_new_item' => 'ニュース記事を新規追加', 10 'edit_item' => 'ニュース記事を編集する', 11 'new_item' => 'ニュース記事', 12 'all_items' => 'ニュース記事一覧', 13 'view_item' => 'ページを確認', 14 'search_items' => '検索する', 15 'not_found' => 'ニュース記事が見つかりませんでした。', 16 'not_found_in_trash' => 'ゴミ箱内にニュース記事が見つかりませんでした。' 17 ), 18 'public' => true, 19 'has_archive' => true, 20 'menu_position' => 5, 21 'supports' => array( 22 'title', 23 'editor', 24 'thumbnail', 25 //'author', 26 //'custom-fields', 27 //'revisions', 28 ), 29 'taxonomies' => array('news_cat') 30 ); 31 register_post_type('news', $params); 32 /* カスタムタクソノミーを定義 */ 33 register_taxonomy( 34 'news_cat', 35 'news', 36 array( 37 'label' => 'カテゴリー', 38 'hierarchical' => true,//カテゴリタイプ 39 'rewrite' => array('slug' => 'news/cat') 40 ) 41 ); 42} 43//コーディネーション実績 44add_action('init', 'add_coordination_post_type'); 45function add_coordination_post_type() { 46 $params = array( 47 'labels' => array( 48 'name' => 'これまでのコーディネイト実績', 49 'singular_name' => 'コーディネーション実績記事', 50 'add_new' => '新規追加', 51 'add_new_item' => 'コーディネーション実績を新規追加', 52 'edit_item' => 'コーディネーション実績を編集する', 53 'new_item' => 'コーディネーション実績', 54 'all_items' => 'コーディネーション実績一覧', 55 'view_item' => 'ページを確認', 56 'search_items' => '検索する', 57 'not_found' => 'コーディネーション実績が見つかりませんでした。', 58 'not_found_in_trash' => 'ゴミ箱内にコーディネーション実績が見つかりませんでした。' 59 ), 60 'public' => true, 61 'has_archive' => true, 62 'menu_position' => 5, 63 'supports' => array( 64 'title', 65 'editor', 66 'thumbnail', 67 //'author', 68 //'custom-fields', 69 //'revisions', 70 ), 71 'taxonomies' => array('coordination_cat') 72 ); 73 register_post_type('coordination', $params); 74 /* カスタムタクソノミーを定義 */ 75 register_taxonomy( 76 'coordination_cat', 77 'coordination', 78 array( 79 'label' => 'カテゴリー', 80 'hierarchical' => true,//カテゴリタイプ 81 'rewrite' => array('slug' => 'coordination/cat') 82 ) 83 ); 84}
上記を記述後にwp管理画面の「パーマリンク設定」でカスタム構造 を「/%category%/%post_id%/」に更新しましたら、カスタム投稿ページの準備が完了しました。
ここでの記事名は
・ニュース記事
/news/任意のslug名
・実績記事
/coordination/任意のslug名
となります。これを
↓
・ニュース記事
/news/自動で与えられるID番号
・実績記事
/coordination/任意のslug名
にしたいのです。
そのために、下記の記述をしました。
php
1//カスタムポストのパーマリンクを投稿IDにする 2function myposttype_permalink($post_link, $id = 0, $leavename) { 3 global $wp_rewrite; 4 $post = get_post($id); 5 if ( is_wp_error( $post ) ) 6 return $post; 7 $newlink = $wp_rewrite->get_extra_permastruct($post->post_type); 8 $newlink = str_replace('%'.$post->post_type.'%', $post->ID, $newlink); 9 $newlink = home_url(user_trailingslashit($newlink)); 10 return $newlink; 11 } 12 add_filter('post_type_link', 'myposttype_permalink', 1, 3); 13 function myposttype_rewrite() { 14 global $wp_rewrite; 15 $wp_rewrite->add_rewrite_tag('%news%', '([0-9]+)', 'post_type=news&p='); 16 } 17 add_action('init', 'myposttype_rewrite');
記述後にwp管理画面の「パーマリンク設定」を更新しましたら、ページ名の設定がニュースも実績もID名になってしまいました。ID名になるのはニュースだけでよいのです。どのように記述を変えればよいのでしょうか。
wordpressにお詳しい方でお分かりになる方がいらっしゃいましたらご教示をお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/28 08:27
退会済みユーザー
2017/08/28 08:52
2017/08/28 09:08
退会済みユーザー
2017/08/28 09:20
2017/08/28 09:28