如何在Wordpress永久链接中包含自定义字符串? [英] How to include a custom string in a Wordpress permalink?

查看:156
本文介绍了如何在Wordpress永久链接中包含自定义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于WordPress的永久链接是如何工作的,我有些困惑,尤其是在Wordpress自己的用法之外.我的永久链接是:

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%

但是在single.php中,我想放置另一个指向页面本身的链接,但使用不同的查询字符串.单击该链接后,永久链接结构可能如下所示:

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%

我想从$_GET['action']获取值,所以:

$_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
}

这意味着,我想可选地解析$_GET['action'].如果即使在查询字符串中也可以使用它,我也无法解析它,那么我希望页面能够正确呈现.

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.

要完成此操作,我应该在哪里实际工作?另外,我该如何形成<a>标签的链接?通常我们以这种方式进行链接:

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.

先谢谢了.

推荐答案

保持永久链接结构不变,并查看我对自定义重写的回答规则.

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(
        '(.+)/(.+)/?$' => '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处可用.

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

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

在查看儿童页面或页面附件时,这可能会产生不良影响.您可以通过在重写中传递一个标识符来解决这个问题,

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.

这篇关于如何在Wordpress永久链接中包含自定义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆