Home Backend Development PHP Tutorial Code example of wordpress custom url parameters to implement routing function_PHP tutorial

Code example of wordpress custom url parameters to implement routing function_PHP tutorial

Jul 13, 2016 pm 05:17 PM
url wordpress code Function parameter and study accomplish regular of Research Example customize expression routing

After two days of learning regular expressions and studying the routing function of WordPress, I successfully implemented the custom WordPress routing function. The following is the implementation of routing rules.
If there are custom url parameters, to be passed through routing, the parameters must be added through the wordpress function:

Copy code 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

At the same time, you also need to use the wordpress function to obtain the parameters on the page:

Copy code The code is as follows:

$type=isset($wp_query->query_vars['type'])? urldecode($wp_query->query_vars['type']):'';

Copy code The code is as follows:

//Routing rules - sorting based on time and the latest entries in each category
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');

Copy code The code is as follows:

//Routing Rules - Category
add_rewrite_rule('^text/?(/ page/([0-9]+)?)?/?$','index.php?cat=2&paged=$matches[2]','top'); //Corresponding 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');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621710.htmlTechArticleAfter two days of learning regular expressions and studying wordpress routing functions, I successfully implemented custom wordpress routing Function, the following is the implementation of routing rules. If there is a custom...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ parameter type safety checking ensures that functions only accept values ​​of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

How to match multiple words or strings using Golang regular expression? How to match multiple words or strings using Golang regular expression? May 31, 2024 am 10:32 AM

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

Does wordpress need to be registered? Does wordpress need to be registered? Apr 16, 2024 pm 12:07 PM

WordPress requires registration. According to my country's "Internet Security Management Measures", websites that provide Internet information services within the country must register with the local provincial Internet Information Office, including WordPress. The registration process includes steps such as selecting a service provider, preparing information, submitting an application, reviewing and publishing, and obtaining a registration number. The benefits of filing include legal compliance, improving credibility, meeting access requirements, ensuring normal access, etc. The filing information must be true and valid, and must be updated regularly after filing.

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

See all articles