0

下面的例程插入一个页面,将永久链接设置为 postname,然后尝试更新永久链接设置。

但是,当我查看新创建的页面时,我仍然收到 404 错误。让它消失的唯一方法是点击设置>永久链接管理器(只需访问页面就可以了,我什至不必保存)。

那是怎么回事?

//create and insert a post
$my_post['post_content'] = "This is the content";
$my_post['post_type'] = 'page';
$my_post['post_status'] = 'publish';
wp_insert_post($my_post);

// 包括负责 .htaccess 更新的文件 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();
4

3 回答 3

0

只给你一个建议...在调试此问题之前禁用任何 WordPress 缓存功能。在使用任何 WP 缓存时,我经常遇到永久链接问题。

于 2010-03-10T01:53:28.217 回答
0

您的代码在哪里,在插件或主题中?

以下插件在最新版本的 WP 中适用于我。如果不可写,它将无法工作.htaccess,但我认为您对此无能为力。

function my_plugin_activate() {
    $test_post = array(
        'post_title' => 'Lorem ipsum',
        'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipisicing …',
        'post_status' => 'publish',
        'post_type' => 'post'
    );
    wp_insert_post($test_post);

    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

register_activation_hook(__FILE__, 'my_plugin_activate');
于 2010-03-10T02:19:03.920 回答
0

问题出在你的 apache 上。

  1. 在 /etc/apache2/sites-available/000-default.conf 上添加这一行。

    <Directory "/var/www/html"> AllowOverride all 要求所有授予

    </目录>

  2. 然后在终端上运行:sudo a2enmod rewrite。

于 2016-04-26T22:11:36.590 回答