如何在 WordPress 永久链接中包含自定义字符串?

发布于 2024-09-10 00:08:37 字数 867 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-09-17 00:08:37

保持固定链接结构不变,然后查看我对自定义重写规则的回答

您可以像这样调整代码;

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(
        '(.+)/(.+)/?

现在页面 my_page 应该可以在 http://example.com/whatever/my_pagehttp://example.com/my_page 访问。

您可以使用 get_query_var('my_action') 获取 whatever 的值。

免责声明

在查看子页面或页面附件时,这可能会产生不良影响。您可以通过在重写中传递一个标识符来解决这个问题,其效果是:

http://example.com/my_identifier/whatever/page

注意:如果您想这样做,您将需要编辑重写规则。每次更改代码时,您都需要重新保存永久链接结构以“刷新”规则。

=> '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');

现在页面 my_page 应该可以在 http://example.com/whatever/my_pagehttp://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;

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(
        '(.+)/(.+)/?

Now the page my_page should be available at http://example.com/whatever/my_page and http://example.com/my_page.

You can get the value of whatever using get_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;

http://example.com/my_identifier/whatever/page

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.

=> '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');

Now the page my_page should be available at http://example.com/whatever/my_page and http://example.com/my_page.

You can get the value of whatever using get_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文