1

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

1

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(
        'movie',
        'book',
        'album'
    );

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

}
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' );
1
  • 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? Commented May 18, 2017 at 9:05
0

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.

register_post_type(
    'my_post_type',
    array(
        'hierarchical' => true,
        'public' => true,
        'rewrite' => array(
            'slug'       => 'my_post_type',
            'with_front' => false,
        ),
        'supports' => array(
            'page-attributes' /* This will show the post parent field */,
            'title',
            'editor',
            'something-else',
        ),
        // Other arguments
    )
);
5
  • 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 Commented 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. Commented 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 Commented 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 Commented 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? Commented 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.