以编程方式在WordPress中添加页面

时间:2012-01-13 作者:byronyasgur

我正在尝试通过编程方式向WordPress添加页面。我正在使用此代码:

 function add_media_page(){
if(!(is_page(\'My New Post\'))){

// Create post object
  $my_post = array(
     \'post_title\' => \'My new Post\',
     \'post_content\' => \'This is my post.\',
     \'post_status\' => \'publish\',
     \'post_author\' => 1,
     \'post_type\' => [\'page\']
  );

// Insert the post into the database
  wp_insert_post( $my_post );
}
}
add_action( \'init\', \'add_media_page\' ); 
但是,当我使用数组“post\\u type”=>[“page”]中的最后一行时,它会导致管理员崩溃并显示一个空白页面。有人知道这里发生了什么吗

3 个回复
最合适的回答,由SO网友:byronyasgur 整理而成

“post\\u type”=>[“page”]错误;它应该是“post\\u type”=>“page”

SO网友:Rob Vermeer

\'post_type\' => [\'page\'] 必须是\'post_type\' => \'page\'. 请看下面的示例:http://codex.wordpress.org/Function_Reference/wp_insert_post#Example

SO网友:markratledge

在函数中的代码中尝试此操作。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);

结束

相关推荐