wordpress 自定义路由及展示页

注册domain/test这个路由

wordpress 有重写url的方法,叫 add_rewrite_rule()。在function.php中加入如下代码段:

// 添加路由重写,每次修改完记得在wp-admin后台“设置”-》“固定链接”=》“保存”才能生效
add_action('init', 'theme_functionality_urls');
function theme_functionality_urls() {
add_rewrite_rule('^test','index.php?test=1','top');
}
这段代码的意思就是把domain?test=1改写成domain/test,top意思是把这个规则放到最前面。

响应domain/test这个请求

  • 获取到这个test的值
add_action('query_vars', 'test_add_query_vars');
function test_add_query_vars($public_query_vars){
$public_query_vars[] = 'test';
return $public_query_vars;
}
这段代码的意思是在执行到query_vars这个钩子的时候,给$public_query_vars数组里面添加一个test字段,这个test字段就是当访问domain?test=1的时候的test的字段。当添加test到$public_query_vars之后,会检查每个请求url里是否包含test字段。
  • 模板载入规则
//模板载入规则
add_action("template_redirect", 'test_template_redirect');
function test_template_redirect(){
global $wp;
global $wp_query;
$reditect_page = $wp_query->query_vars['test'];
if ($reditect_page == "1"){
include(TEMPLATEPATH.'/test/test.php');
die();
}
}
这段代码的意思是这样的,首先,这个TEMPLATEPATH是你的主题路径,连着/test/test.php相当于在你的主题目录下新建了一个test目录,test目录里有一个test.php文件。

wordpress 钩子执行顺序

muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
**init**
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_stypes
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_selection
wp
template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts

在管理屏幕上触发钩子的典型顺序

muplugins_loaded - this is the first hook available to must-use plugins
registered_taxonomy
registered_post_type
plugins_loaded - this is the first hook available to regular plugins
auth_cookie_valid
set_current_user
load_textdomain
sanitize_comment_cookies
setup_theme
unload_textdomain
after_setup_theme - this is the first hook available to themes
init
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_styles
wp_default_scripts
debug_bar_enqueue_scripts
wp_loaded - This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
auth_redirect
admin_menu
pre_get_users
pre_user_query
admin_init
... lots of other stuff

其它参考

最新文章

  1. Ajax:一种网页开发技术(Asynchronous Javascript + XML)
  2. [KOJ0574NOIP200406合并果子]
  3. cocos2d_随手篇1_关于ccTouchBegan的调用
  4. STM32系统时钟
  5. Ubuntu下安装搜狗拼音输入法
  6. 5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)
  7. Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
  8. 深度解密Go语言之关于 interface 的10个问题
  9. VS 2013Ultimate 开发过程中遇到的问题——listbox的隐藏问题,combobox.textchanged的中文问题
  10. Linux内存管理(二)
  11. C++ 解析Json——jsoncpp
  12. 2019-04-12-day031-进程基础
  13. (转)Java中equals和==、hashcode的区别
  14. 《全栈性能Jmeter》-5JMeter负载与监听
  15. 如何使用网格搜索来优化深度学习模型中的超参数(Keras)
  16. git从安装到使用
  17. The Little Prince-11/27
  18. Nuts & Bolts Problem
  19. Android基于RecyclerView实现高亮搜索列表
  20. PHP——初学,基础

热门文章

  1. GMS的概述
  2. spring boot自定义类配置绑定在配置文件中自动提示
  3. 180. 连续出现的数字 + MySql + 连续出现数字 + 多表联合查询
  4. 基于4H-SIC的先进集成电路用n型LDMOS晶体管
  5. burpsuite 隐藏 detectportal.firefox.com
  6. C语言入门-ide的概念介绍及codeblocks编辑器安装汉化
  7. P2516 [HAOI2010]最长公共子序列 题解(LCS)
  8. [ONTAK2010] Peaks 加强版
  9. Paperfolding HDU - 6822
  10. 全量同步Elasticsearch方案之Canal