Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two custom rewrite rules; after reading allot of great posts here I found a way to make them work.

The question is this: Is there a way to merge this code? I'm not sure that the way I wrote it is optimal:

In my functions.php I have the code of the 1st rule:

add_action('init', 'rewrite_cat'); // Rewrite Category
    add_action('query_vars', 'rewrite_query_vars_cat'); 
    add_filter('template_include', 'rewrite_template_include_cat'); 
    function rewrite_cat(){ 
        add_rewrite_rule( '([^/]+)/([0-9]+)/?', 'index.php?catname=$matches[1]&currentpage=$matches[2]', 'top' );
    }
    function rewrite_query_vars_cat($query_vars){    
        $query_vars[] = 'currentpage'; 
        $query_vars[] = 'catname'; 
        return $query_vars; 
        } 
    function rewrite_template_include_cat($template){     
        if (get_query_var('currentpage') || get_query_var('catname')){        
            $template = locate_template(array('category.php'));
        }     
            return $template; 
        }

After, this is the code of the 2nd rule:

add_action('init', 'rewrite_perf'); // Rewrite Perf
    add_action('query_vars', 'rewrite_query_vars_perf'); 
    add_filter('template_include', 'rewrite_template_include_perf'); 
    function rewrite_perf(){ 
        add_rewrite_rule('cam/([^/]+)/([^/]+)/?', 'index.php?name=cam&sgcatname=$matches[1]&perf=$matches[2]', 'top');
    }
    function rewrite_query_vars_perf($query_vars){  
        $query_vars[] = 'sgcatname'; 
        $query_vars[] = 'perf'; 
        return $query_vars; 
        } 
    function rewrite_template_include_perf($template){     
        if (get_query_var('sgcatname') || get_query_var('perf')){        
            $template_perf = locate_template(array('single.php'));
        }     
            return $template; 
        }       
share|improve this question

1 Answer 1

up vote 1 down vote accepted

This is the working code:

add_action('init', 'rewrite_init'); // Rewrite
    add_action('query_vars', 'rewrite_query_vars'); 
    add_filter('template_include', 'rewrite_template_include'); 
    function rewrite_init(){ 
        add_rewrite_rule('([^/]+)/([0-9]+)/?$', 'index.php?catname=$matches[1]&currentpage=$matches[2]', 'top');
        add_rewrite_rule('cam/([^/]+)/?$', 'index.php?name=cam&perf=$matches[1]', 'top');
    }
    function rewrite_query_vars($query_vars){    
        $query_vars[] = 'currentpage'; 
        $query_vars[] = 'catname'; 
        $query_vars[] = 'perf'; 
        return $query_vars; 
    } 
    function rewrite_template_include($template){     
        if(get_query_var('currentpage') || get_query_var('catname')){        
            $template = locate_template(array('category.php'));
        } elseif(get_query_var('perf')){
            $template = locate_template(array('single.php'));
        }
            return $template; 
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.