admin管理员组

文章数量:1122846

I am trying to get my two custom post types to work properly. Let's say the two Custom Post Types (CPT) are books and reviews.

now i like

mydomain/books/the-great-gatsby/reviews/33

to show all the reviews for book the great gatsby.

In general mydomain/books/{title-book}/reviews/{book_id}

I have made bought CPT's and i have a two page templates (page-books.php and page-reviews.php). Page-books.php works as expected for page-reviews.php i am testing with a hardcoded $book_id and this works. But i need to make page-reviews.php dynamic. In my local setup page-reviews.php has a page_id=227. I am putting

echo 'Book id '.$_REQUEST['book_id'];

in page-reviews.php for testing purpose

so i tried

add_rewrite_rule('^books/([^/]*)/reviews/([^/]*)/?','index.php?page_id=227&book_id=$matches[2]','top');

but i get a 404 page.

Any tips?

I am trying to get my two custom post types to work properly. Let's say the two Custom Post Types (CPT) are books and reviews.

now i like

mydomain.com/books/the-great-gatsby/reviews/33

to show all the reviews for book the great gatsby.

In general mydomain.com/books/{title-book}/reviews/{book_id}

I have made bought CPT's and i have a two page templates (page-books.php and page-reviews.php). Page-books.php works as expected for page-reviews.php i am testing with a hardcoded $book_id and this works. But i need to make page-reviews.php dynamic. In my local setup page-reviews.php has a page_id=227. I am putting

echo 'Book id '.$_REQUEST['book_id'];

in page-reviews.php for testing purpose

so i tried

add_rewrite_rule('^books/([^/]*)/reviews/([^/]*)/?','index.php?page_id=227&book_id=$matches[2]','top');

but i get a 404 page.

Any tips?

Share Improve this question edited Sep 12, 2014 at 21:22 alex asked Sep 12, 2014 at 20:55 alexalex 1,1123 gold badges17 silver badges39 bronze badges 1
  • ok resaving the permalinks and i got redirected, but the book_id isn't parsed even with a static value &book_id=123 parsing $_GET['book_id'] is empty. – alex Commented Sep 12, 2014 at 21:28
Add a comment  | 

1 Answer 1

Reset to default 0

ok this i didn't know i had to "register" variable to use

function my_add_rewrite_rules() {
    global $wp,$wp_rewrite;
    $wp->add_query_var('book_id');
    add_rewrite_rule('^books/([^/]*)/reviews/([0-9]+)/?$', 'index.php?page_id=227&bookid_id=$matches[2]', 'top');
    // Once you get working, remove this next line
  $wp_rewrite->flush_rules(false);  

}
add_action('init', 'my_add_rewrite_rules');

and in page-reviews.php i am using

$book_id = $GLOBALS['wp']->query_vars['book_id'];

One last question: how do i fix my breadcrumbs as the books/.../reviews is not displayed in the breadcrumbs?

本文标签: rewrite rulesAddrewriterule doesn39t seem to work