How to redirect /page/subpage to a template file? (2 posts)

  1. Lucas Martins
    Posted 7 years ago #

    I have a page called "courses", which uses a template file for this page.
    The slug is courses.

    But, when I try to access myblog.com/courses/course1 I get an 404 error. Is there any way to make this page call the template file set for courses? without making changes in 404.php or creating new subpages thru the admin panel.

    Thank you!

  2. Lucas Martins
    Posted 7 years ago #

    I found a way to do this:

    function courses_rewrite_rules($wp_rewrite) {
    	$new_rules = array(
    		'courses/(.*)' => 'index.php?page_id=34'
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    function courses_flush_rules() {
    	global $wp_rewrite;
    add_action('init', 'courses_flush_rules');
    add_action('generate_rewrite_rules', 'courses_rewrite_rules');

    This piece of code does what I want. But everytime a wordpress page (not necessarily /courses ) is requested it delete and re-create the rewrite rules on the database, even if theres no change. I think this can cause some problem in the future.


    function courses_set_rule() {
    add_rewrite_rule('courses/(.*)', 'index.php?page_id=34', 'top');
    add_action('init', 'courses_set_rule');

Topic Closed

This topic has been closed to new replies.

About this Topic