C'est La Vie

    人生无彩排,每一天都是现场直播!

    wordpress添加重写规则,构建新页面

    添加翻译规则。首先我们前面介绍了url的翻译规则,我们要往翻译规则中添加一条自己的翻译规则 add_actio […]

    添加翻译规则。首先我们前面介绍了url的翻译规则,我们要往翻译规则中添加一条自己的翻译规则

    add_action('generate_rewrite_rules', 'hliang_rewrite_rules' );
    /**********重写规则************/
    function hliang_rewrite_rules( $wp_rewrite ){
    	$new_rules = array(    
    		'my-account/?$' => 'index.php?my_custom_page=hello_page',
    	); //添加翻译规则
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    	//php数组相加   
    }

    添加 $public_query_vars

    /*******添加query_var变量***************/
    add_action('query_vars', 'hliang_add_query_vars');
    function hliang_add_query_vars($public_query_vars){
    	$public_query_vars[] = 'my_custom_page'; //往数组中添加添加my_custom_page
    
    	return $public_query_vars;
    }
    

    添加模板载入规则

    //模板载入规则
    add_action("template_redirect", 'hliang_template_redirect');
    function hliang_template_redirect(){
    	global $wp;
    	global $wp_query, $wp_rewrite;
    
    	//查询my_custom_page变量
    	$reditect_page =  $wp_query->query_vars['my_custom_page'];
    	//如果my_custom_page等于hello_page,则载入user/helloashu.php页面
    	//注意 my-account/被翻译成index.php?my_custom_page=hello_page了。
    	if ($reditect_page == "hello_page"){
    	    include(TEMPLATEPATH.'/user/helloashu.php');
    	    die();
    	}
    }

    更新重写规则

    /***************激活主题更新重写规则***********************/
    add_action( 'load-themes.php', 'hliang_flush_rewrite_rules' );
    function hliang_flush_rewrite_rules() {
    	global $pagenow, $wp_rewrite;
    	if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
    		$wp_rewrite->flush_rules();
    }

    OK,到了这里,到后台重新激活你的主题,就能看到前面图示的效果了。

    发表回复

    您的电子邮箱地址不会被公开。 必填项已用*标注