Hun*_*der 1 php wordpress permalinks
我对WordPress的永久链接如何工作有点困惑,特别是超出了Wordpress自己的用法.我的永久链接就像:
%post_id%-%post_name%
Run Code Online (Sandbox Code Playgroud)
但是single.php我希望将另一个链接放到页面本身但是使用不同的查询字符串.单击它时,永久链接结构可能如下所示:
%mystring%-%post_id%-%post_name%
Run Code Online (Sandbox Code Playgroud)
我想从中得到价值$_GET['action'],所以:
$_GET['action'] = %mystring%
Run Code Online (Sandbox Code Playgroud)
我的计划是将它解释为:
if('xx' == $_GET['action']){
//do xx stuff
} else if ('yy'==$_GET['action']){
//do yy stuff
} else {
//show the single post as a single.php always shows
}
Run Code Online (Sandbox Code Playgroud)
这意味着,我想解析$_GET['action']可选的.如果我不解析它,即使它在查询字符串中可用,我希望页面正确呈现.
为了完成这项工作,我应该在哪里工作?另外我如何形成<a>标签的链接?通常我们这样做链接:
<a href="'.the_permalink().'">TEXT</a>
Run Code Online (Sandbox Code Playgroud)
但你已经知道了,我需要在帖子的原始永久链接之前添加一些文字.
提前致谢.
保留固定链接结构,并查看我关于自定义重写规则的答案.
您可以像这样调整代码;
function my_rewrite_rules($rules)
{
global $wp_rewrite;
// the key is a regular expression
// the value maps matches into a query string
$my_rule = array(
'(.+)/(.+)/?$' => 'index.php?pagename=matches[2]&my_action=$matches[1]'
);
return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');
function my_query_vars($vars)
{
// this value should match the rewrite rule query paramter above
// I recommend using something more unique than 'action', as you
// could collide with other plugins or WordPress core
$my_vars = array('my_action');
return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');
Run Code Online (Sandbox Code Playgroud)
现在页面my_page应该在http://example.com/whatever/my_page和http://example.com/my_page.
您可以获得whatever使用的价值get_query_var('my_action').
在查看子页面或页面附件时,这可能会产生不良影响.你可以通过在重写中传递一个标识符来解决这个问题.
http://example.com/my_identifier/whatever/page
Run Code Online (Sandbox Code Playgroud)
注意:如果您希望这样做,则需要编辑重写规则.每次更改代码时,都需要重新保存永久链接结构以"刷新"规则.
| 归档时间: |
|
| 查看次数: |
2658 次 |
| 最近记录: |