如何以编程方式在WordPress中创建帖子

use*_*026 11 html php wordpress

我想知道我如何以编程方式为大约七个不同的帖子在WordPress中创建帖子?

我试过了:

 <div class="demo">     
<?php global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post); ?>
</div>
Run Code Online (Sandbox Code Playgroud)

它没有显示任何东西?

San*_*hra 15

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
Run Code Online (Sandbox Code Playgroud)

  • [wp_insert_post() 的文档](https://developer.wordpress.org/reference/functions/wp_insert_post/) 具有(许多)更多选项。 (2认同)