【问题标题】:How to Set POST permalink/slug in wordpress using wp_insert_post如何使用 wp_insert_post 在 wordpress 中设置 POST 永久链接/slug
【发布时间】:2011-08-19 08:36:14
【问题描述】:

我正在使用wp_insert_post() 编写简单的脚本以在博客中发布文章。 但这里有一个问题,我想让 URL 的 Titleslug 不同。

如何做到这一点?

例如:

标题:如何让你的饮食成功

蛞蝓:7-ways-to-make-succes-Diet

【问题讨论】:

    标签: wordpress slug permalinks


    【解决方案1】:

    post_title 设置标题,post_name 设置标题。 所以:

    // 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 );
    

    【讨论】:

    • 嗨,是否可以将帖子 ID 附加到 post_name 的末尾,以便生成的 slug 看起来像“hello-world-123”,其中 123 是插入的帖子 ID?跨度>
    • 这是我尝试过的,但收到错误'post_name' => '$title'.'-'$pid',
    • @anagio 试试添加这个:, 'post_name' => sanitize_title_with_dashes($title,'','save')
    猜你喜欢
    • 2019-02-13
    • 2017-05-06
    • 2011-01-24
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多