WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Lucas Martins
    Member
    Posted 5 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
    Member
    Posted 5 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;
    	$wp_rewrite->flush_rules();
    }
    
    add_action('init', 'courses_flush_rules');
    add_action('generate_rewrite_rules', 'courses_rewrite_rules');

    BUT
    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.

    THE SOLUTION

    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