今天介绍的是一些通过代码编写,在WordPress博客里添加各种数据(比如日志、评论、分类等)的简单方法。
插入文章
在WordPress里插入一篇文章非常简单。要用到的是wp_insert_post()函数,这个函数以数组作为参数。
如果你想试验一下这个函数,可以把下面的代码复制到functions.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);
$new_post数组里的参数包括:
- post_title: 所插入文章的文章名
- post_content: 所插入文章的内容
- post_status: 文章的发表状态(已发布、草稿等)
- post_date: 以date()为日期或另行自定义日期
- post_author: 文章作者ID
- post_type: 可以是日志、页面或其他自定义文章类型
- post_category: 分类ID数组
插入评论
在WordPress里插入评论也相当容易。同样是把下面的代码复制到functions.php文件。
$data = array( 'comment_post_ID' => 1, 'comment_author' => 'admin', 'comment_author_email' => 'admin@admin.com', 'comment_author_url' => 'http://www.catswhocode.com', 'comment_content' => 'Lorem ipsum dolor sit amet...', 'comment_author_IP' => '127.0.0.1', 'comment_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3', 'comment_date' => date('Y-m-d H:i:s'), 'comment_date_gmt' => date('Y-m-d H:i:s'), 'comment_approved' => 1,);$comment_id = wp_insert_comment($data);
wp_insert_comment()函数和wp_insert_post()一样,也是以数组作为参数:
- comment_post_ID: 被评论文章的ID
- comment_author:评论作者名
- comment_author_email: 评论作者的Email地址
- comment_author_url: 评论作者的网址
- comment_content: 评论内容
- comment_author_IP: 评论作者的IP地址
- comment_agent: 评论者浏览器的用户代理
- comment_date: 评论发表日期
- comment_date_gmt: 评论的GMT时间
- comment_approved: 评论是否已被审核?1为已审核,0为待审核
为某篇文章添加分类
WordPress的内置函数wp_set_object_terms()可以将文章添加到各分类中。
我们需要做的就是创建一个数组,数组里包含需要添加的分类ID,然后:
$category_ids = array(4, 5, 6);wp_set_object_terms( $post_id, $category_ids, 'category');
wp_set_object_terms()用了三个参数:文章ID, 分类ID的数组、分类类型(此处为"category")
为某篇文章添加标签
添加标签甚至不需要用到新函数,wp_set_object_terms()函数同样可以实现添加标签的效果。
$tag_ids = array(7, 8, 9);wp_set_object_terms( $post_id, $tag_ids, 'post_tag');
文章发表时自动生成自定义字段
在文章发表时自动生成自定义字段会为我们节省很多时间。
依然是把下面的代码复制到functions.php文件,然后发表新文章时,就会有自动生成的自定义字段了。
function add_custom_field_automatically($post_ID) { global $wpdb; if(!wp_is_post_revision($post_ID)) { add_post_meta($post_ID, 'field-name', 'custom value', true); }}add_action('publish_page', 'add_custom_field_automatically');add_action('publish_post', 'add_custom_field_automatically');
原理:首先生成一个函数。该函数用以确认新发表文章不是某篇旧文的修改版,然后添加一个自定义字段,字段名为field-name,字段值为custom value。之后,调用一个hook来确保每当新文章或者新页面发布时都会调用add_custom_field_automatically()函数。
波比源码 » 通过代码在WordPress里插入各种数据
levofloxacin cost levaquin generic
buy altace generic astelin 10 ml canada azelastine 10ml us
order tacrolimus 5mg generic order generic ropinirole 2mg order ursodiol for sale
imdur oral telmisartan for sale micardis 20mg cheap
cost gloperba quick hits free online slots casino game
sildenafil 100mg pills for sale tadalafil online tadalafil 20mg
tadalafil 10mg sale buy diclofenac 100mg generic buy indomethacin 50mg online cheap
biaxin drug clonidine price meclizine 25mg cost
order spiriva 9 mcg pills spiriva 9 mcg usa buy terazosin 1mg generic
cost albuterol 100 mcg albuterol order online cipro 500mg drug
order tadalafil 10mg online cheap Canadian pharmacy cialis vegas casino online
play money poker online real online blackjack poker game
best online blackjack real money best poker online real money online casinos for real money
buy adalat 10mg online perindopril 4mg uk buy generic fexofenadine 180mg
assignment website buy arava 20mg online buy azulfidine 500mg generic
doxycycline 200mg generic ventolin online order order cleocin 150mg pill
purchase asacol pill buy irbesartan 150mg generic order avapro 300mg generic
order generic lanoxin order micardis online molnunat online buy
purchase clobetasol online cheap buy buspar sale buy cordarone generic
order amoxil 250mg pill stromectol 3mg usa ivermectin for human
order generic requip 1mg purchase rocaltrol without prescription buy generic trandate 100mg
nexium pills buy lasix 100mg without prescription buy furosemide 40mg
minocin over the counter oral minocycline order hytrin 5mg online
glucophage canada nolvadex 20mg cheap nolvadex pills
cheap accutane 10mg buy ampicillin 500mg without prescription order ampicillin 500mg sale
stromectol for human online ed meds order deltasone 5mg online cheap
buy accutane online oral zithromax 250mg azithromycin order online