2

我的主题的自定义选项面板具有以下代码...

` /* 初始化站点选项 */

if(get_option('permalink_structure')==""){update_option('permalink_structure', '/%postname%/');} `

这会检查永久链接选项设置,并且由于 WP 默认值为 "",它会触发 site.com/?p=x 处理程序。这样,如果用户还没有设置默认的永久链接,我的脚本会为他们设置永久链接,方法是将永久链接设置为帖子名称。或者至少我是这么想的……

但是,我有几个拥有我的模板的人告诉我,在第一次安装时,他们在 pages 上收到 404 错误

显然,解决方法是物理导航到永久链接页面,然后单击“保存更改”(即使当您第一次点击此页面时,永久链接也会出现,就好像它已正确输入到“自定义”字段中一样。

有谁知道为什么会这样?除了在上面的代码中调用 update_options() 时会发生什么之外,它们可能是 db 中确定永久链接的另一个设置吗?

4

2 回答 2

1

好吧,这可能是因为您正在更新数据库表 (permalink_structure) 中的值,而 .htaccess 保持不变,这就是未加载 mod_rewrite 并且用户在页面上收到 404 错误的原因。

我相信 WordPress 还会在 .htaccess 中添加重写规则,以便在您单击管理面板中的“保存更改”时启用永久链接。让我挖掘一下,看看 WP 到底在做什么。


编辑。

好的,这是正在执行您要完成的操作的代码:

<?php

if (get_option('permalink_structure') == "")
{
    // Including files responsible for .htaccess update
    require_once(ABSPATH . 'wp-admin/includes/misc.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');

    // Prepare WordPress Rewrite object in case it hasn't been initialized yet
    if (empty($wp_rewrite) || !($wp_rewrite instanceof WP_Rewrite))
    {
        $wp_rewrite = new WP_Rewrite();
    }

    // Update permalink structure
    $permalink_structure = '/%postname%/';
    $wp_rewrite->set_permalink_structure($permalink_structure);

    // Recreate rewrite rules
    $wp_rewrite->flush_rules();
}
于 2009-12-03T19:57:41.737 回答
0

wp_rewrite 似乎没有任何效果。用户仍然需要在永久链接屏幕上手动单击“保存选项”。

我想我会在更新期间在该页面上运行 firebug,以查看 update_options 显然缺少的设置。

当要更新的选项是 permalink_structure 时,这似乎是 update_options 中的一个错误。

有人不同意吗?

于 2009-11-14T14:52:27.833 回答