i am new in wordpress and i am sorry for my english. May be my title is wrong. sorry for that.

My question is like that.

I have too many custom post types. I need some pages for a specific custom post type.

For example: I have musics, books and movies. I want to create a template for them like that; http://example.com/book/a-book-name/author http://example.com/movie/a-movie-name/director

How can I do that? Thank you for helping and sorry for my english.

2 Answers 2


You can add a rewrite tag and rules that capture anything after the post name:

function wpd_add_rewrites(){

    add_rewrite_tag( '%my_page%', '(.+)' );

    $post_types = array(

    foreach( $post_types as $post_type ){
            '^' . $post_type . '/([^/]*)/([^/]*)/?',
            'index.php?post_type=' . $post_type . '&name=$matches[1]&my_page=$matches[2]',

add_action( 'init', 'wpd_add_rewrites' );

Don't forget, you must flush rewrite rules after adding / changing them. You can do this quickly by just visiting the Settings > Permalinks page.

You can then check the value of my_page anywhere after the wp action:

echo get_query_var( 'my_page' );
  • It worked but now I need filter them. For example: if 'my_page' is 'author' or 'example' show my page else show 404 page. How can i do it? May 18, 2017 at 9:05

Ok, look. When you create the custom post. you must add this lines. If you see, you add .. 'supports' => array('page-attributes'... this option show a select in your post for choose the parent post.

        'hierarchical' => true,
        'public' => true,
        'rewrite' => array(
            'slug'       => 'my_post_type',
            'with_front' => false,
        'supports' => array(
            'page-attributes' /* This will show the post parent field */,
        // Other arguments
  • Ok, it finely work when i add a new post for book's author. but i already added informations for that post with using custom fields. so i dont need and i dont wanna add new post. you can think like comments. i want show comments on example.com/book/a-book/comments May 18, 2017 at 2:11
  • I understand what you say, but I guess that's more complicated and would require a lot more code. I guess using the url passing variables could be an option. Sorry I can not be of more help. May 18, 2017 at 2:16
  • Do you know how can I use the url passing variables in wordpress? Actually it can be help for me May 18, 2017 at 2:19
  • Yes, easy. In your file functions, you create de variable function add_custom_query_var( $vars ){ $vars[] = "a"; return $vars; } add_filter( 'query_vars', 'add_custom_query_var' ); you can call in other page with: '$my_a = get_query_var( 'a' );' you can use like this: example.com/movie/a-movie-name/?a=name_autor May 18, 2017 at 2:22
  • thank i did. now i need to use add_rewrite_rule for SEO link so can you help me for that? May 18, 2017 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.