11

我正在尝试为 WordPress 编写一个插件,但我在 wp_rewrite 功能方面遇到了一些问题。

我想通过 URL 传递变量(例如:www.mysite.com/WordPress?variable=helloall)使一个页面显示为多个

但是我想保持永久链接结构完整,所以我希望 URL 显示为:

www.mysite.com/WordPress/helloall

然后我希望能够使用 slug 并使用它来搜索我的数据库。(就像你会使用 $_GET 如果我使用我首先提到的一般方法)


我在网上找到了一些教程,并且到目前为止还能够使其正常工作。我相信我的问题是由于对如何正确编写规则缺乏了解。

我用过这个教程:

http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

而且我大部分时间都尝试使用相同的代码。我能够制定规则,但他们似乎不想为我工作

谁能告诉我正确的格式可以做到这一点?

编辑

这是我目前的功能

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array
    (
        '(.?.+?)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2).'&page='.
        $wp_rewrite->preg_index(3),

        '(.?.+?)/(.*?)/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2)
    );
    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_action('generate_rewrite_rules', 'add_rewrite_rules');

解决方案

我已经想通了,我打算将其发布为答案,但似乎我目前无法及时回答我自己的问题,因此我正在编辑原始帖子:

首先,我在上面发布的代码是正确的,但是它不起作用的原因是因为我没有刷新规则,我使用以下代码执行此操作:

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;

$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');

我的新问题是我的代码运行得有点好,重定向所有页面而不是只重定向我想要的页面,这意味着不会显示任何子页面,这有点问题,但是我已经解决了一个小问题编辑:

function add_rewrite_rules( $wp_rewrite ) 
{
  $new_rules = array
  (
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
  );
  // Always add your rules to the top, to make sure your rules have priority
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

所以我关于 wp_rewrite 功能的最终代码如下:

function add_rewrite_rules( $wp_rewrite ) 
{
$new_rules = array
(
    '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2).'&page='.
    $wp_rewrite->preg_index(3),

    '(testpage)/(.*?)/?$' => 'index.php?pagename='.
    $wp_rewrite->preg_index(1).'&varname='.
    $wp_rewrite->preg_index(2)
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function query_vars($public_query_vars)
{
$public_query_vars[] = "varname";
return $public_query_vars;
}

function ebi_flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');

我希望这可以在将来节省其他人的时间。

4

1 回答 1

8

我想出了如何让它在特定页面名称上工作,这适用于将来遇到问题的任何人:

function add_rewrite_rules( $wp_rewrite ) 
{
    $new_rules = array
    (
        '(testpage)/(.+?)/page/?([0-9]{1,})/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2).'&page='.
        $wp_rewrite->preg_index(3),

        '(testpage)/(.*?)/?$' => 'index.php?pagename='.
        $wp_rewrite->preg_index(1).'&varname='.
        $wp_rewrite->preg_index(2)
    );
    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function query_vars($public_query_vars)
{
    $public_query_vars[] = "varname";

    return $public_query_vars;
}

function ebi_flush_rewrite_rules()
{
    global $wp_rewrite;

    $wp_rewrite->flush_rules();
}

add_action( 'init', 'flush_rewrite_rules');
add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars', 'query_vars');
于 2012-10-30T17:30:10.763 回答