WordPress:Function Reference/wp insert post

来自站长百科
Fludlen讨论 | 贡献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.