やりたいこと
カスタム投稿ページのパーマリンクを親ページにはスラッグを、子ページには数字を表示できる様にしたい。
(タイトルと内容を編集しました 1/17 11:55)
appleというページを投稿した場合
現在
親ページ exsample.jp/products/123
子ページ exsample.jp/products/456
この様にしたい
親ページ exsample.jp/products/apple/123
子ページ exsample.jp/products/apple/456
試したこと
以下のコードを使用して、カスタム投稿ページ(product)以降を数字にすることができました。
ただ、今の状態だと親ページも、子ページも数字で表記されてしまいます。
add_action('init', 'myposttype_rewrite'); function myposttype_rewrite() { global $wp_rewrite; $queryarg = 'post_type=products&p='; $wp_rewrite->add_rewrite_tag('%products_id%', '([^/]+)',$queryarg); $wp_rewrite->add_permastruct('products', '/products/%products_id%', 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; }
以下の辺りをスラッグに表示する様書き加えたらいいと思うのですが、今の自分のスキルでは難しかったです。
$queryarg = 'post_type=products&p='; $wp_rewrite->add_rewrite_tag('%products_id%', '([^/]+)',$queryarg); $wp_rewrite->add_permastruct('products', '/products/%products_id%', false);
何かヒントを頂くことは出来ないでしょうか
どうぞ宜しくお願いします。
あなたの回答
tips
プレビュー