• wordpress 自定义路由及展示页


    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
    
    

    其它参考

    正因为来之不易,所以才有了后来的倍加珍惜。
  • 相关阅读:
    剑指Offer--反转链表
    剑指Offer--链表中倒数第k个结点
    面向对象的六原则一法则
    常见错误汇总
    记人生第一次CF体验
    Game of Credit Cards
    Shell Game (模拟)
    数列分块入门 1 LibreOJ
    范德蒙恒等式
    C. Vasya and String (尺取法)
  • 原文地址:https://www.cnblogs.com/jjxhp/p/10982782.html
Copyright © 2020-2023  润新知