如何在 WordPress 永久链接中包含自定义字符串?
我对 WordPress 的永久链接如何工作有点困惑,尤其是在 WordPress 自己的用法之外。我的永久链接如下:
%post_id%-%post_name%
但在 single.php
中,我想将另一个链接放置到页面本身,但具有不同的查询字符串。单击时,永久链接结构可能如下所示:
%mystring%-%post_id%-%post_name%
我想从 $_GET['action']
获取值,所以:
$_GET['action'] = %mystring%
我的计划是将其解释为:
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
}
这意味着,我想解析$_GET['action']
可选。如果我不解析它,即使它在查询字符串中可用,我希望页面能够正确呈现。
那么为了完成这项工作,我实际上应该在哪里工作?另外,如何形成 标记的链接?通常我们这样建立链接:
<a href="'.the_permalink().'">TEXT</a>
但你已经知道,我需要在帖子的原始永久链接之前添加一些文本。
提前致谢。
I am a bit confused about how WordPress's permalink works, especially beyond Wordpress's own usage. My permalinks are like:
%post_id%-%post_name%
But in single.php
I want to put another link to page itself but with different query string. When it is clicked the permalink structure may look like:
%mystring%-%post_id%-%post_name%
I want to get the value from $_GET['action']
, so:
$_GET['action'] = %mystring%
my plan is to interpret it like:
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
}
that means, I want to parse the $_GET['action']
optionally. If I do not parse it even if it is available in query string, I want the page to be rendered correctly.
So to get this done, where should I actually work? Also how do I form the link for <a>
tag? Usually we make link this way:
<a href="'.the_permalink().'">TEXT</a>
but you already know, I need to add some text before the original permalink of post.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持固定链接结构不变,然后查看我对自定义重写规则的回答。
您可以像这样调整代码;
现在页面
my_page
应该可以在http://example.com/whatever/my_page
和http://example.com/my_page
访问。您可以使用
get_query_var('my_action')
获取whatever
的值。免责声明
在查看子页面或页面附件时,这可能会产生不良影响。您可以通过在重写中传递一个标识符来解决这个问题,其效果是:
注意:如果您想这样做,您将需要编辑重写规则。每次更改代码时,您都需要重新保存永久链接结构以“刷新”规则。
Leave your permalink structure as it was and check out my answer on custom rewrite rules.
You could adapt the code like so;
Now the page
my_page
should be available athttp://example.com/whatever/my_page
andhttp://example.com/my_page
.You can get the value of
whatever
usingget_query_var('my_action')
.Disclaimer
This may have undesired effects when viewing children pages or page attachments. You could get around this by passing an identifier in your rewrite, something to the effect of;
Note: You will need to edit the rewrite rule if you wish to do this. Every time you make changes to the code you will need to re-save your permalink structure to 'flush' the rules.