This article mainly introduces the sample code for implementing the routing function in wordpress. You can refer to the Code to implement your own routing function after two days of Regular Expression learning, and study the wordpress routing function, the custom wordpress routing function is successfully implemented. The following describes the implementation of routing rules.
If you want to pass custom url parameters through a route, you must use the wordpress function to add the parameters:
The Code is as follows:
// Add query_args
Function add_query_vars ($ aVars ){
$ AVars [] = 'score ';
$ AVars [] = 'type'; // represents the name of the product category as shown in the URL
Return $ aVars;
}
Add_filter ('query _ vars', 'add _ query_vars '); // wordpress Filter
You also need to use the wordpress function to obtain the parameters on the page:
The Code is as follows:
$ Type = isset ($ wp_query-> query_vars ['type'])? Urldecode ($ wp_query-> query_vars ['type']): '';
The Code is as follows:
// Routing rules-sort by time and other latest entries
Function add_rewrite_rules ($ aRules ){
$ ANewRules = array (
'Text/([^ latest] [^/] + )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 2 & score = $ matches [1] & paged = $ matches [3] ',
'Image/([^ latest] [^/] + )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 3 & score = $ matches [1] & paged = $ matches [3] ',
'Video/([^ latest] [^/] + )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 4 & score = $ matches [1] & paged = $ matches [3] ',
'Resource/([^ latest] [^/] + )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 5 & score = $ matches [1] & paged = $ matches [3] ',
'Text/(latest )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 2 & type = $ matches [1] & paged = $ matches [3] ',
'Image/(latest )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 3 & type = $ matches [1] & paged = $ matches [3] ',
'Video/(latest )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Cat = 4 & type = $ matches [1] & paged = $ matches [3] ',
'Resource/(latest )/? $ '=> 'Index. php? Cat = 5 & type = $ matches [1] ',
'(Month )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Score = $ matches [1] & paged = $ matches [3] ',
'(24hr )/? (/Page/([0-9] + )?)? /? $ '=> 'Index. php? Score = $ matches [1] & paged = $ matches [3] ',
);
$ ARules = $ aNewRules + $ aRules;
Return $ aRules;
}
Add_filter ('rewrite _ rules_array ', 'add _ rewrite_rules ');
The Code is as follows:
// Routing rule-type
Add_rewrite_rule ('^ text /? (/Page/([0-9] + )?)? /? $ ', 'Index. php? Cat = 2 & paged = $ matches [2] ', 'top'); // category ID
Add_rewrite_rule ('^ image /? (/Page/([0-9] + )?)? /? $ ', 'Index. php? Cat = 3 & paged = $ matches [2] ', 'top ');
Add_rewrite_rule ('^ video /? (/Page/([0-9] + )?)? /? $ ', 'Index. php? Cat = 4 & paged = $ matches [2] ', 'top ');
Add_rewrite_rule ('^ resource /? (/Page/([0-9] + )?)? /? $ ', 'Index. php? Cat = 5 & paged = $ matches [2] ', 'top ');