Sample code for routing using wordpress custom url parameters _ PHP Tutorial

Source: Internet
Author: User
Sample code for routing using wordpress custom url parameters. After two days of regular expression learning and studying wordpress routing functions, the custom wordpress routing function is successfully implemented. The following describes the implementation of routing rules. If you have learned a custom regular expression for two days and studied the wordpress routing function, you have successfully implemented the custom wordpress routing function. The following describes how to implement 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 ');

Bytes. If there is a custom...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.