【问题标题】:Adding /us/ permalink to every parmalink in WordPress将 /us/ 永久链接添加到 WordPress 中的每个永久链接
【发布时间】:2021-01-14 21:42:57
【问题描述】:

我想要的很简单。我只想将 /us/ 添加到 WordPress 中的每个永久链接。

例如:如果链接是 example.com/a-page/ 或 example.com/a-post/ 我希望它自动转换为 example.com/us/a-page/ 和 example.com/us/a-post/。

/us/ 应该添加到每个帖子、页面、CPT、产品、类别、所有内容中。

最简单的解决方案是将整个 WordPress 放在 /us 目录中。但我想知道是否有一种方法可以在不将其放入目录的情况下做到这一点。

【问题讨论】:

    标签: php wordpress url-rewriting


    【解决方案1】:

    您可以为此使用 preg_replace。

    执行正则表达式搜索和替换

    <?php
    add_action( 'wp_loaded', function() {
      function wp_custom_( $subject ) {
        if( ! is_admin() ) {
          $search = [
            '/href="(.*?)\/"/',
            '/href="(.*?)"/',
            '/href=\'(.*?)\/\'/',
            '/href=\'(.*?)\'/',
            // ... etc.
          ];
          $replace = [
            'href="$1"',
            'href="$1/us/"',
            'href=\'$1\'',
            'href=\'$1/us/\'',
          ];
          $subject = preg_replace( $search, $replace, $subject );
          return $subject;
        };
      };
      ob_start( 'wp_custom_' );
    } ); ?>
    

    了解详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2017-04-19
      • 2017-12-07
      • 1970-01-01
      • 2013-01-16
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多