WordPress: Function Reference/wp insert post:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第1行: 第1行:
==Description==
==描述==
==描述==
This function inserts posts (and pages) in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. It takes an object as its argument and returns the post ID of the created post (or <tt>0</tt> if there is an error).


这个函数向数据库插入文章(和网页),清理了一些变数,做一些检查,填入缺失的变数,如时间/日期等等。这个函数将一个对象作为变数,并且返回创建的文章的ID(如果有错误的话,返回是<tt>0</tt>)。
这个函数向数据库插入文章(和网页),清理了一些变数,做一些检查,填入缺失的变数,如时间/日期等等。这个函数将一个对象作为变数,并且返回创建的文章的ID(如果有错误的话,返回是<tt>0</tt>)。
==Usage==
==用法==
==用法==


%%% <?php wp_insert_post( $post ); ?> %%%
%%% <?php wp_insert_post( $post ); ?> %%%
%%% <?php wp_insert_post( $post ); ?> %%%
==Example==


==例子==
==例子==
Before calling wp_insert_post() it is necessary to create an object (typically an array) to pass the necessary elements that make up a post. The wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.


调用wp_insert_post()之前,有必要创建一个对象(通常是一个数组),来传递组成文章的必要成分。wp_insert_post()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。
调用wp_insert_post()之前,有必要创建一个对象(通常是一个数组),来传递组成文章的必要成分。wp_insert_post()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。
The user can provide more elements than are listed here by simply defining new keys in the database. The keys should match the names of the columns in the wp_posts table in the database.


除了这儿定义的数据库中的新的关键字之外,用户还可以提供更多的内容。关键字应该与数据库中的wp_posts表格中的栏名称相匹配。
除了这儿定义的数据库中的新的关键字之外,用户还可以提供更多的内容。关键字应该与数据库中的wp_posts表格中的栏名称相匹配。
<pre>
// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
// Insert the post into the database
  wp_insert_post( $my_post );
</pre>
<pre>
<pre>
// 创建文章object
// 创建文章object
第53行: 第23行:
</pre>
</pre>


The default list referred to above is defined in the function body. It is as follows:


关于上面内容的默认列表定义在函数主体中,如下:
关于上面内容的默认列表定义在函数主体中,如下:
<pre>
$defaults = array(
  'post_status' => 'draft',
  'post_type' => 'post',
  'post_author' => $user_ID,
  'ping_status' => get_option('default_ping_status'),
  'post_parent' => 0,
  'menu_order' => 0,
  'to_ping' =>  '',
  'pinged' => '',
  'post_password' => '',
  'guid' => '',
  'post_content_filtered' => '',
  'post_excerpt' => ''
);
</pre>
<pre>
<pre>
$defaults = array(
$defaults = array(
第93行: 第41行:
);
);
</pre>
</pre>
==Parameters==


==参数==
==参数==
{{Parameter|$post|object|An object representing the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database.}}


{{Parameter|$post|object|一个object代表一篇文章的组成部分。这些组成部分与数据库中wp_post表格中的栏的名称是一对一的关系。}}
{{Parameter|$post|object|一个object代表一篇文章的组成部分。这些组成部分与数据库中wp_post表格中的栏的名称是一对一的关系。}}
==Return==


==返回==
==返回==
The ID of the post if the post is successfully added to the database. Otherwise returns <tt>0</tt>.


如果文章成功添加到数据库,会返回文章的ID。否则会返回<tt>0</tt>。
如果文章成功添加到数据库,会返回文章的ID。否则会返回<tt>0</tt>。
==Related==
==相关的==
==相关的==
[[WordPress:Function Reference/wp delete attachment|wp_delete_attachment()]],
[[WordPress:Function Reference/wp delete attachment|wp_delete_attachment()]],
[[WordPress:Function Reference/wp get attachment url|wp_get_attachment_url()]],
[[WordPress:Function Reference/wp get attachment url|wp_get_attachment_url()]],
[[WordPress:Function Reference/wp insert attachment|wp_insert_attachment()]]
[[WordPress:Function Reference/wp insert attachment|wp_insert_attachment()]]
[[WordPress:Function Reference/wp delete attachment|wp_delete_attachment()]],
[[WordPress:Function Reference/wp get attachment url|wp_get_attachment_url()]],
[[WordPress:Function Reference/wp insert attachment|wp_insert_attachment()]]
{{Copyedit}}
{{Copyedit}}

2008年7月24日 (四) 15:18的最新版本

描述[ ]

这个函数向数据库插入文章(和网页),清理了一些变数,做一些检查,填入缺失的变数,如时间/日期等等。这个函数将一个对象作为变数,并且返回创建的文章的ID(如果有错误的话,返回是0)。

用法[ ]

%%% <?php wp_insert_post( $post ); ?> %%%

例子[ ]

调用wp_insert_post()之前,有必要创建一个对象(通常是一个数组),来传递组成文章的必要成分。wp_insert_post()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。

除了这儿定义的数据库中的新的关键字之外,用户还可以提供更多的内容。关键字应该与数据库中的wp_posts表格中的栏名称相匹配。

// 创建文章object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;

// 将文章插入到数据库中
  wp_insert_post( $my_post );


关于上面内容的默认列表定义在函数主体中,如下:

$defaults = array(
  'post_status' => 'draft',
  'post_type' => 'post',
  'post_author' => $user_ID,
  'ping_status' => get_option('default_ping_status'),
  'post_parent' => 0,
  'menu_order' => 0,
  'to_ping' =>  '',
  'pinged' => '',
  'post_password' => '',
  'guid' => '',
  'post_content_filtered' => '',
  'post_excerpt' => ''
);

参数[ ]

返回[ ]

如果文章成功添加到数据库,会返回文章的ID。否则会返回0

相关的[ ]

wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()