Optimaler Code für zwei add_rewrite_rule’s

Ich habe zwei benutzerdefinierte Rewrite-Regeln; Nachdem ich hier eine Menge toller Beiträge gelesen hatte, fand ich einen Weg, sie zum functionieren zu bringen.

Die Frage ist: Gibt es eine Möglichkeit, diesen Code zusammenzuführen? Ich bin mir nicht sicher, ob die Art, wie ich es geschrieben habe, optimal ist:

In meiner functions.php habe ich den Code der 1. Regel:

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; } 

Danach ist dies der Code der 2. Regel:

 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; } 

Solutions Collecting From Web of "Optimaler Code für zwei add_rewrite_rule’s"

Dies ist der Arbeitscode:

 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; }