【问题标题】:Wordpress rewrite add base prefix to pages onlyWordpress 重写仅向页面添加基本前缀
【发布时间】:2013-07-10 22:18:52
【问题描述】:

我在尝试完成项目时遇到了一个问题。

  1. 我将当前的永久链接结构设置为 /%postname%/
  2. 我创建了自己的函数,只为帖子添加前缀,因此我的帖子被重写为 /{prefix}/%postname%/。

我的问题是我想更改 页面 的永久链接,就像我对帖子所做的那样,所以我的页面将具有 /{prefix}/%pagename%/ 之类的前缀。

我尝试过但没有成功:

  1. 重新声明 PAGES 帖子类型并设置重写 slug。
  2. 尝试将自定义重写规则添加为函数,但没有成功:

    $rewrite_rules += array('mycustomprefix/(.+?)/([0-9]+)/([^/]+)/([^/]+)/?$' =>'index .php?pagename=$matches[1]',

这可能吗?有没有遇到同样问题的开发者?

【问题讨论】:

    标签: wordpress url-rewriting rewrite permalinks


    【解决方案1】:

    我发现这个解决方案更适合我......而且它的代码也更简洁。

    add_action( 'init', 'custom_page_rules' );
    
    function custom_page_rules() {
      global $wp_rewrite;
      $wp_rewrite->page_structure = $wp_rewrite->root . 'your-page-prefix/%pagename%'; 
    }
    

    我在这里找到了代码:http://wpforce.com/change-wordpress-page-permalinks/

    【讨论】:

      【解决方案2】:

      对于任何感兴趣的人,我已通过以下方式解决了我的问题:

      function change_author_permalinks() {
      global $wp_rewrite;
      // Change the value of the author permalink base to whatever you want here
      $wp_rewrite->author_base = '';
      // Change the value of the page permalink base to whatever you want here
      $wp_rewrite->page_structure = 'static/%pagename%';
      $wp_rewrite->flush_rules();
      }
      add_action('init','change_author_permalinks');
      

      希望这对其他人有所帮助,因为我在任何地方都找不到任何帮助。有关您可以通过这种方式进行哪些更改的更多信息,请查看http://codex.wordpress.org/Class_Reference/WP_Rewrite

      【讨论】:

        【解决方案3】:

        在将这个重写添加到您的 functions.php 之后,您是否更新了永久链接结构?它对我有用:)

        add_filter( 'page_rewrite_rules', 'customprefix_page_rewrite_rules' );
        function customprefix_page_rewrite_rules( $rewrite_rules )
        {
            end( $rewrite_rules );
            $last_pattern = key( $rewrite_rules );
            $last_replacement = array_pop( $rewrite_rules );
            $rewrite_rules +=  array(
                'mycustomprefix/(.+?)/?$' => 'index.php?pagename=$matches[1]',
                $last_pattern => $last_replacement,
            );
            return $rewrite_rules;
        }
        

        【讨论】:

        • 我认为这是我的问题,没有刷新规则。当我更改为自定义重写规则时,我无法访问 WP-Admin,因为我使用的是主题我的登录,这会将我重定向到静态页面进行登录/注册...非常感谢!
        • 虽然这可行,但它不会更新菜单永久链接(使用默认的 WP 菜单)。另外,当我使用get_permalink() 时,它不会添加前缀。
        猜你喜欢
        • 1970-01-01
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多