您可以在页面标题上使用sanitize\\u title():
http://codex.wordpress.org/Function_Reference/sanitize_title
然后你可以试试这样:
$my_page_title = "My page name";
$my_page_slug = sanitize_title($my_page_title);
// or you can set the slug you want:
//$my_page_slug = "my-slug-for-this-page";
$my_page_content = "This is my page content";
// Create page object
$my_page = array(
\'post_title\' => $my_page_title,
\'post_name\' => $my_page_slug,
\'post_content\' => $my_page_content,
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'post_author\' => 1,
);
// Insert the page into the database
wp_insert_post( $my_page );
更新页面时,假设您有权访问页面ID,请使用以下代码防止重复条目:
$my_page = array(
\'ID\' => $my_page_id // current page ID
\'post_title\' => $my_page_title,
\'post_name\' => $my_page_slug,
\'post_content\' => $my_page_content,
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'post_author\' => 1,
);
// Update page
wp_update_post( $my_page );
请注意,只需将更改后的值传递给wp\\u update\\u post(因此,如果只更改了页面内容,则应传递
$my_page_content
仅限)