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 |1 Answer
Reset to default 0ok 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
版权声明:本文标题:rewrite rules - Add_rewrite_rule doesn't seem to work? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292698a1928990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
&book_id=123
parsing $_GET['book_id'] is empty. – alex Commented Sep 12, 2014 at 21:28