2

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

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

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

我试过但没有奏效:

  1. 重新声明 PAGES 帖子类型并设置重写 slug。
  2. 尝试将自定义重写规则添加为函数,但它不起作用:

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

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

4

3 回答 3

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

于 2013-07-15T10:13:24.097 回答
0

将此重写添加到您的 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;
}
于 2013-07-12T15:16:09.097 回答
0

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

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/

于 2014-04-24T14:00:05.163 回答