###前提・実現したいこと
カスタム投稿タイプのパーマリンクの設定を
投稿記事のidにしたいと思っています。
以下のサイトを参考にfunctions.phpに入力したのですが、
投稿記事へのリンクがおかしくなり、400 Bad Requestがでました。
参考サイト:http://miso0nok.net/2013/10/web/wordpress-custom-post-type-permlink/
投稿記事のURLは以下のようになってしまいます。
https://ドメイン/treatment/%treatment%
ワードプレス管理画面のパーマリンク設定は、
カスタム構造で「/%post_id%」が設定してあります。
できればプラグインを使用せずに、投稿idで正しくリンクさせたいです。
よろしくお願い致します。
###該当のソースコード
functions.php
add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'treatment', [ 'labels' => [ 'name' => __(‘treatment’), 'singular_name' => __( '記事一覧' ) ], 'public' => true, 'supports' => ['title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields'], 'menu_position' => 5, 'has_archive' => true, 'rewrite' => true, ] ); /* post_id.htmlにRewrite */ add_action('init', 'myposttype_rewrite'); function myposttype_rewrite() { global $wp_rewrite; $queryarg = 'post_type=treatment&p='; $wp_rewrite->add_rewrite_tag('%treatment%', '([^/]+)',$queryarg); $wp_rewrite->add_permastruct('treatment', '/treatment/%treatment_id%.html', false); } add_filter('post_type_link', 'myposttype_permalink', 1, 3); function myposttype_permalink($post_link, $id = 0, $leavename) { global $wp_rewrite; $post = &get_post($id); if ( is_wp_error( $post ) ) return $post; $newlink = $wp_rewrite->get_extra_permastruct($post->post_type); $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink); $newlink = home_url(user_trailingslashit($newlink)); return $newlink; } /* post_id.htmlにRewrite */
###補足情報(言語/FW/ツール等のバージョンなど)
WordPress 4.6.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/07 11:54
2016/10/07 12:26
2016/10/07 13:44
2016/10/12 12:42