简体   繁体   English

更改带标签的帖子的永久链接结构

[英]Change permalink structure for tagged posts

The current permalink structure for the posts that I am trying to modify includes a date, and it is configured in the settings page like: "/%year%/%monthnum%/%day%/%category%/%postname%/" 我要修改的帖子的当前永久链接结构包含一个日期,并且在设置页面中对其进行了配置,例如:“ /%year%/%monthnum%/%day%/%category%/%postname%/”

An example post url: http://example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/ 帖子网址示例: http : //example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/

My problem is that I can't figure out how to modify this permalink structure for posts that are tagged with a specific tag, so for example, if I have a tag "permalink-struc-2", and I add it to my post, I would like to then rewrite the permalink to: http://example.com/category/child-category/long-post-slug-is-printed-here/ The date should be stripped from the post permalink. 我的问题是我无法弄清楚如何为带有特定标签的帖子修改此永久链接结构,例如,如果我有一个标签“ permalink-struc-2”,并将其添加到我的帖子中,然后将永久链接重写为: http : //example.com/category/child-category/long-post-slug-is-printed-here/日期应从永久链接中删除。

I've tried using a wp filter "post_link" 我尝试使用wp过滤器“ post_link”

add_filter('post_link', array($this, 'change_permalink_date_structure'), 1, 3); add_filter('post_link',array($ this,'change_permalink_date_structure'),1,3);

But what ends up happening is that the url is changed, but when I try accessing the post from the shortened url, it is still redirected to the original one - > http://example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/ 但是最终发生的是该URL被更改,但是当我尝试从缩短的URL访问该帖子时,它仍被重定向到原始URL- > http://example.com/2016/09/11/category/儿童类别/长子弹被印在这里/

Could anyone help on this issue please? 任何人都可以在这个问题上提供帮助吗?

Thanks 谢谢

You can try to use htaccess mod rewrite. 您可以尝试使用htaccess mod重写。 You will have to add one rule for each url. 您将必须为每个网址添加一个规则。 For example: 例如:

 RewriteRule /category/child-category/long-post-slug-is-printed-here/ /2016/09/11/category/child-category/long-post-slug-is-printed-here/ [L]

Thanks to everyone that tried to help on this, I managed to find a way to do what I needed. 感谢所有尝试提供帮助的人,我设法找到了一种方法来做自己需要的事情。 Basically the I wrote a rewrite rule for wp with add_rewrite_rule() It works for post url's that don't have the date: 基本上,我用add_rewrite_rule()为wp编写了一个重写规则,它适用于没有日期的发布网址:

  $rewrite_rule_regex = '^(?!([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/)(.+?)/([^/]+)(?:/([0-9]+))?/?$';

add_rewrite_rule($rewrite_rule_regex, 'index.php?category_name=$matches[4]&name=$matches[5]&page=$matches[6]&istaggedcalendar=1', 'bottom');

Also, this new rewrite rule had to have a higher priority than the last (universal) wp rewrite rule (it didn't work if my rewrite rule was last, and if it was set to "top" some other rewrite rules didn't work): 另外,此新的重写规则必须具有比上一个(通用)wp重写规则更高的优先级(如果我的重写规则是最后一个,并且如果将其设置为“ top”,则它不起作用,其他一些重写规则则不起作用)工作):

add_filter( 'rewrite_rules_array', array($this, 'rewrite_rulles_array_look')); add_filter('rewrite_rules_array',array($ this,'rewrite_rulles_array_look'));

// * set the priority of our rewrite rule to not interfere with other pages

function rewrite_rules_array_look($r_array) {

  $rew_rule = $r_array[$this->rewrite_rule_regex];

  unset($r_array[$this->rewrite_rule_regex]);

  $res = array_slice($r_array, 0, count($r_array)-1, true) +

      array($this->rewrite_rule_regex => $rew_rule) +

      array_slice($r_array, count($r_array)-1, count($r_array) , true) ;


  return $res;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM