问题
Hello how do I create custom posts inside my index.php file so when someone installs the template it automatically creates like 5 posts?
I tied this:
<?php // Create post object
$my_post = array(
'post_title' => 'My post1',
'post_content' => 'This is my post8.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?>
But it didn't work. What I am trying to do is create custom posts inside <div> tags. So when the users installs the theme they will already be created with the theme and the post will display individually.
For example:
<div class="demo">
<?php // Create post object
$my_post = array(
'post_title' => 'My post1',
'post_content' => 'This is my post8.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?>
</div>
<div class="description">
<span class="big"><?php // Create post object
$my_post = array(
'post_title' => 'My post2',
'post_content' => 'This is my post7.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?> </span>
</div>
回答1:
Looks like you're using the example from wp_insert_post().
- Make sure you actually have the categories with IDs
8and39. - use something like
$var = wp_insert_post($your_args). Then echo$varto get the ID of the post that was created.
来源:https://stackoverflow.com/questions/14599781/wordpress-auto-create-posts