下面的例程插入一个页面,将永久链接设置为 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();