WordPress: Function Reference/wp insert post:修订间差异
(新页面: ==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 it...) |
无编辑摘要 |
||
第1行: | 第1行: | ||
==Description== | ==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). | 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>)。 | |||
==Usage== | ==Usage== | ||
==用法== | |||
%%% <?php wp_insert_post( $post ); ?> %%% | |||
%%% <?php wp_insert_post( $post ); ?> %%% | %%% <?php wp_insert_post( $post ); ?> %%% | ||
==Example== | ==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. | 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()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。 | |||
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. | 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表格中的栏名称相匹配。 | |||
<pre> | <pre> | ||
// Create post object | // Create post object | ||
第20行: | 第38行: | ||
wp_insert_post( $my_post ); | wp_insert_post( $my_post ); | ||
</pre> | </pre> | ||
<pre> | |||
// 创建文章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 ); | |||
</pre> | |||
The default list referred to above is defined in the function body. It is as follows: | The default list referred to above is defined in the function body. It is as follows: | ||
关于上面内容的默认列表定义在函数主体中,如下: | |||
<pre> | <pre> | ||
$defaults = array( | $defaults = array( | ||
第38行: | 第75行: | ||
); | ); | ||
</pre> | </pre> | ||
<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> | |||
==Parameters== | ==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|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表格中的栏的名称是一对一的关系。}} | |||
==Return== | ==Return== | ||
==返回== | |||
The ID of the post if the post is successfully added to the database. Otherwise returns <tt>0</tt>. | The ID of the post if the post is successfully added to the database. Otherwise returns <tt>0</tt>. | ||
如果文章成功添加到数据库,会返回文章的ID。否则会返回<tt>0</tt>。 | |||
==Related== | ==Related== | ||
==相关的== | |||
[[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()]] | |||
[[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()]] | ||
{{Copyedit}} | |||
{{Copyedit}} | {{Copyedit}} |
2008年7月19日 (六) 14:21的版本
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 0 if there is an error).
这个函数向数据库插入文章(和网页),清理了一些变数,做一些检查,填入缺失的变数,如时间/日期等等。这个函数将一个对象作为变数,并且返回创建的文章的ID(如果有错误的话,返回是0)。
Usage
用法
%%% <?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()会找出这些的默认列表,但是用户需要提供标题和内容,否则数据库写操作就会被破坏。
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表格中的栏名称相匹配。
// 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 );
// 创建文章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 );
The default list referred to above is defined in the function body. It is as follows:
关于上面内容的默认列表定义在函数主体中,如下:
$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' => '' );
$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' => '' );
Parameters
参数
Return
返回
The ID of the post if the post is successfully added to the database. Otherwise returns 0.
如果文章成功添加到数据库,会返回文章的ID。否则会返回0。
Related
相关的
wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()
wp_delete_attachment(), wp_get_attachment_url(), wp_insert_attachment()
This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.
This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.