前提・実現したいこと
wordpressのカスタム投稿をfunctions.php
で2つ定義しています。
これらの投稿ページは、single-seminar.php
とsingle-magazine.php
にそれぞれの処理を書いて表示させようと思っています。
seminar
の記事はsingle-seminar.php
の内容が表示されるのですが、
magazine
の記事はsingle-magazine.php
の内容が表示されずに、archive.php
の内容が表示されてしまいます。
カスタム投稿のパーマリンクを投稿タイトルから数字ベースに変更しているのですが、
その記述をコメントアウトすると、magazine
の記事でもsingle-magazine.php
の内容がちゃんと表示されます。
おそらく、カスタム投稿のパーマリンクを投稿タイトルから数字ベースに変更している箇所のコードにミスがあるのかなと思っているのですが、それがわかりません。
wp管理画面>設定>パーマリンクの設定
カスタム構造の/%category%/%post_id%
で設定しています。
functions.php
// ================================================================================ // カスタム投稿タイプ追加 // ================================================================================ function my_post_type() { register_post_type( 'seminar', array( 'label' => 'セミナー', 'labels' => array( 'name' => 'セミナー', 'singular_name' => 'セミナー', 'all_items' => 'セミナー一覧', ), 'public' => true, 'has_archive' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'custom-fields', 'revisions' ), ) ); register_post_type( 'magazine', array( 'label' => 'マガジン', 'labels' => array( 'name' => 'マガジン', 'singular_name' => 'マガジン', 'all_items' => 'マガジン一覧', ), 'public' => true, 'has_archive' => true, 'menu_position' => 6, 'supports' => array( 'title', 'editor', 'custom-fields', 'revisions', 'thumbnail', 'author', ), ) ); } add_action( 'init', 'my_post_type' ); // ================================================================================ // カスタム投稿のパーマリンクを数字ベースに変更 // ================================================================================ add_filter( 'post_type_link', 'my_post_type_link', 1, 2 ); function my_post_type_link( $link, $post ){ if ( 'seminar' === $post->post_type ) { return home_url( '/seminar/' . $post->ID ); } elseif ( 'magazine' === $post->post_type ) { return home_url( '/magazine/' . $post->ID ); } else { return $link; } } add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array' ); function my_rewrite_rules_array( $rules ) { $new_rules = array( 'seminar/([0-9]+)/?$' => 'index.php?post_type=seminar&p=$matches[1]', 'magazine/([0-9]+)/?$' => 'index.php?post_type=magazine&p=$matches[2]', ); return $new_rules + $rules; }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/16 09:45
2019/08/16 09:56 編集