Wordpress 之 Rewrite Rules
-----------------------------------------------------------------
场景描述:
自定义wp主题中,添加了个关于页面(about.php) 。
目的:
顺利访问 http://<domain>/about.php
-----------------------------------------------------------------
解决方案:在主题的functions.php文件中, 自定义重写规则 rewrite_rules
-----------------------------------------------------------------
Step 1. 添加 generate_rewrite_rules
if ( ! function_exists( 'testthemes_rewrite_rules' ) ) :
/**
* @param WP_Rewrite $wp_rewrite
*/
function testthemes_rewrite_rules( $wp_rewrite ) {
$testthemes_rules = [
'about(.*)$' => 'index.php?my_custom_page=about',
]; $wp_rewrite->rules = $testthemes_rules + $wp_rewrite->rules;
}
endif; // testthemes_rewrite_rules add_action( 'generate_rewrite_rules', 'testthemes_rewrite_rules' );
说明:
pattern=>url格式: 'about(.*)$' => 'index.php?my_custom_page=about'
-----------------------------------------------------------------
Step 2. 添加 query_vars
if ( ! function_exists( 'testthemes_add_query_vars' ) ) :
/**
* @param array $public_query_vars
* @return array
*/
function testthemes_add_query_vars($public_query_vars) {
$public_query_vars[] = 'my_custom_page';
return $public_query_vars;
}
endif;
add_action( 'query_vars', 'testthemes_add_query_vars' );
-----------------------------------------------------------------
Step3. 添加 template_redirect
if ( ! function_exists( 'testthemes_template_redirect' ) ) :
/**
* @void
*/
function testthemes_template_redirect() {
global $wp;
/**@var WP_Query $wp_query*/
global $wp_query;
/**@var WP_Rewrite $wp_rewrite*/
global $wp_rewrite; //查询my_custom_page变量
$my_custom_page = $wp_query->query_vars['my_custom_page'];
switch ($my_custom_page) {
case 'about':
include(TEMPLATEPATH.'/about.php');
die();
}
}
endif;
add_action( 'template_redirect', 'testthemes_template_redirect' );
-----------------------------------------------------------------
Step4. 添加 load-themes.php
if ( ! function_exists('testthemes_flush_rewrite_rules')):
/**
* @void
*/
function testthemes_flush_rewrite_rules(){
/**@var string $pagenow*/
global $pagenow;
/**@var WP_Rewrite $wp_rewrite*/
global $wp_rewrite;
if( 'theme.php' == $pagenow && isset( $_GET['activated'] )) {
$wp_rewrite->flush_rules();
}
}
endif;
add_action( 'load-themes.php', 'testthemes_flush_rewrite_rules' );
-----------------------------------------------------------------
Step5. 重新激活主题
END
Wordpress 之 Rewrite Rules的更多相关文章
- Nginx下WordPress的Rewrite
最近接触WP Super Cache,该插件要求固定链接必须是重写的,故用到Rewrite. 我的是这样配置的: /usr/local/nginx/conf/rewrite/wordpress.con ...
- (视频) 《快速创建网站》 3.2 WordPress多站点及Azure在线代码编辑器 - 扔掉你的ftp工具吧,修改代码全部云端搞定
本文是<快速创建网站>系列的第6篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 访问本系列目录,请点击:http://devopshub.cn/tag ...
- nginx rewrite重写与防盗链配置
nginx rewrite重写规则与防盗链配置方法 时间:2016-02-04 15:16:58来源:网络 导读:nginx rewrite重写规则与防盗链配置方法,rewrite规则格式中flag标 ...
- winserver 2008 r2 iis7.5 实现php wordpress url静态化操作步骤(UrlRewrite实现)
参考网址:http://jingyan.baidu.com/article/cbf0e500ebec582eaa2893d2.html 文中涉及到的程序源码以及配置 详见附件:http://files ...
- How to install Wordpress 4.0 on CentOS 7.0
This document describes how to install and configure Wordpress 4.0 on CentOS 7.0. WordPress started ...
- 黄聪:wordpress中remove_action、add_action、 do_action()的hook钩子都有哪些
原文地址:http://codex.wordpress.org/Plugin_API/Action_Reference muplugins_loaded After must-use plugins ...
- WordPress nginx环境下开启多站点
在wp-config.php插入 define('WP_ALLOW_MULTISITE', true); 进入管理页面安装网络,子目录模式按提示再在wp-config.php插入 define('MU ...
- (视频) 《快速创建网站》 3.2 WordPress多站点及Azure在线编辑器 - 扔掉你的ftp工具吧,修改代码全部云端搞定
本文是<快速创建网站>系列的第6篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 1. 网站管理平台WordPress和云计算平台Azure简介 (6分 ...
- WordPress版微信小程序开发系列(一):WordPress REST API
自动我发布开源程序WordPress版微信小程序以来,很多WordPress站长在搭建微信小程序的过程中会碰到各种问题来咨询我,有些问题其实很简单,只要仔细看看我写的文章,就可以自己解决.不过这些文章 ...
随机推荐
- html5--6-1 引入外部样式表
html5--6-1 引入外部样式表 实例 学习要点 掌握引入外部样式表方法 插入样式的三种方法 内联样式表(行内) 内部样式表(style中) 外部样式表 创建一个外部样式表 在head中使用lin ...
- Tensorflow深度学习之十二:基础图像处理之二
Tensorflow深度学习之十二:基础图像处理之二 from:https://blog.csdn.net/davincil/article/details/76598474 首先放出原始图像: ...
- Opencv— — Bias and Gain
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- ZOJ3201(树形DP)
Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weights of ea ...
- POJ1163(基础线性DP)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42547 Accepted: 25721 De ...
- SQL集锦之IndexOf、LastIndexOf 【转】
DECLARE @Name NVARCHAR (50)SET @Name = '12345.67890ABCDE.FGHIJKLMNOPQRSTUVWXYZTest' DECLARE @Positio ...
- wampServer 设置
设置端口 打开 C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf 文件 找到“Listen 80”和“ServerName localhost:80”,紧接 ...
- HDFS源码分析三-DataNode实现
3. DataNode 实现( 未完待续 )
- (水题)洛谷 - P2439 - 阶梯教室设备利用 - 简单dp
https://www.luogu.org/fe/problem/P2439 很明显时间是一个维度,按照时间顺序决策就行了. dp[i]表示以时间i为结尾所能达到的最长演讲时间. #include & ...
- 洛谷P4717 【模板】快速沃尔什变换(FWT)
传送门 这玩意儿太骚了…… 参考了yyb巨佬的 //minamoto #include<iostream> #include<cstdio> #define ll long l ...