【问题标题】:Change permalink structure for tagged posts更改标记帖子的永久链接结构
【发布时间】:2016-11-21 02:59:28
【问题描述】:

我正在尝试修改的帖子的当前永久链接结构包括一个日期,它在设置页面中配置,如:“/%year%/%monthnum%/%day%/%category%/%postname %/"

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

我的问题是我不知道如何修改带有特定标签的帖子的永久链接结构,例如,如果我有一个标签“permalink-struc-2”,我添加它在我的帖子中,我想将永久链接重写为:http://example.com/category/child-category/long-post-slug-is-printed-here/ 日期应从帖子固定链接中删除。

我尝试过使用 wp 过滤器“post_link”

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

但最终发生的事情是 url 发生了变化,但是当我尝试从缩短的 url 访问帖子时,它仍然被重定向到原来的 -> http://example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/

有人可以帮忙解决这个问题吗?

谢谢

【问题讨论】:

  • 你能告诉我真正想要实现什么吗?
  • 我想更改带有特定标签的帖子的永久链接结构
  • 你当前的网址是什么??
  • 我在问题描述中发布了网址,我无法发布真实的网址,因为该项目是机密的。

标签: php wordpress url-rewriting permalinks


【解决方案1】:

您可以尝试使用 htaccess mod rewrite。您必须为每个 url 添加一个规则。例如:

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

【讨论】:

  • 谢谢你,但它并没有完全解决我的问题,因为要为我想要这个例外的每个帖子编辑 htaccess 文件并不容易。
【解决方案2】:

感谢所有为此提供帮助的人,我设法找到了一种方法来做我需要的事情。基本上我用 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');

此外,这个新的重写规则必须具有比最后一个(通用)wp 重写规则更高的优先级(如果我的重写规则是最后一个,并且如果它被设置为“顶部”一些其他重写规则,它就不起作用了)没用):

add_filter('rewrite_rules_array', array($this, 'rewrite_rules_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;

}

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多