如何使用wp_insert_post在wordpress中设置POST永久链接/ slug

And*_*ndy 9 wordpress permalinks slug

我正在编写简单的脚本wp_insert_post()用于在博客中发表文章.但是这里有一个问题,我想让URL的标题slug不同.

怎么做到这一点?

例如:

标题:如何使您的饮食成功

弹头: 7-ways-to-make-succes-Diet

win*_*nas 26

post_title设置标题,post_name设置slug.所以:

// Create post object
$my_post = array(
     'post_title' => 'How to make your diet success',
     'post_name' => '7-ways-to-make-succes-Diet',
     'post_content' => 'my content',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array(8,39)
  );

// Insert the post into the database
wp_insert_post( $my_post );
Run Code Online (Sandbox Code Playgroud)

  • @anagio尝试添加:`,'post_name'=> sanitize_title_with_dashes($ title,'','save')` (4认同)