Wordpress auto create posts

爷,独闯天下 提交于 2020-01-14 07:01:27

问题


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().

  1. Make sure you actually have the categories with IDs 8 and 39.
  2. use something like $var = wp_insert_post($your_args). Then echo $var to get the ID of the post that was created.


来源:https://stackoverflow.com/questions/14599781/wordpress-auto-create-posts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!