Sample Code _ php instance for routing using wordpress custom url parameters

Source: Internet
Author: User
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 ');

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.