-----------------------------------------------------------------

场景描述:

自定义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

最新文章

  1. tomcat配置性能调优1----server.xml文件详解
  2. JavaBean与Jsp
  3. p4-hlir/test源码 stateful.p4 control_flow_opt.p4
  4. 使用AndroidStudio编译NDK的方法及错误解决方案
  5. LintCode &quot;Maximum Gap&quot;
  6. PHP实现站点pv,uv统计(二)
  7. EIGRP默认路由分发的四种方法
  8. DIH中添加不同的数据源
  9. 自定义HBase的协处理器(Observer)
  10. C# 线程--第四线程实例
  11. CocoaPods 安装和使用
  12. 为什么我的outlook只能收信不能发信,发送测试电子邮件消息: 无法发送此邮件。请在帐户属性中验证电子邮件
  13. Java 中值传递和引用传递 区分
  14. deepin系统下如何设置wifi热点(亲测有效)
  15. LCA—倍增法求解
  16. 【安卓开发】Layout Inflation不能这么用
  17. SYM File
  18. 51-迷宫(一)- java版dfs和bfs
  19. 使用机器学习检测TLS 恶意加密流——业界调研***有开源的数据集,包括恶意证书的,以及恶意tls pcap报文***
  20. WikiBooks/Cg Programming

热门文章

  1. tensorflow sigmoid_cross_entropy_with_logits 函数解释
  2. BZOJ_4809_皇后_爆搜
  3. CF 757E Bash Plays with Functions——积性函数+dp+质因数分解
  4. 实用的MVVM:ImageView
  5. Linux查看CPU《型号..》《内存..》《硬盘..》《系统..》
  6. 【旧文章搬运】KeUserModeCallback用法详解
  7. Python深入浅出property特性属性
  8. Numbers Exchange
  9. Several ports (8005, 8080, 8009) required
  10. 《剑指offer》面试题7—用两个栈实现队列