今天介绍的是一些通过代码编写,在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
generic prednisolone neurontin 100mg pill cost furosemide
purchase doxycycline sale levitra for sale online acyclovir brand
buy zocor 10mg sale cheap phenergan purchase sildalis generic
purchase alfuzosin sale uroxatral 10mg usa diltiazem 180mg usa
purchase zetia sale buy methotrexate for sale oral methotrexate 2.5mg
buy levaquin 500mg generic bupropion online bupropion online buy
cheap zyrtec 10mg order generic sertraline order sertraline pills
atorvastatin tablet viagra ca buy sildenafil 50mg for sale
order tadalafil 20mg sale pills erectile dysfunction hims ed pills
cialis 20mg usa generic cialis sale buy generic ed pills for sale
purchase oral ivermectin absorica online accutane 20mg cheap
buy generic provigil online deltasone 40mg us how to buy prednisone
buy amoxil 500mg online cheap order amoxil 250mg online purchase prednisolone sale
buy cheap accutane buy generic accutane for sale order zithromax sale
order gabapentin 800mg pill buy gabapentin pills for sale vibra-tabs pill
albuterol canada ventolin inhalator online buy synthroid for sale
brand clomiphene 50mg vardenafil 20mg usa order generic plaquenil 200mg
monodox cost amoxiclav order clavulanate where to buy
tenormin 100mg tablet methylprednisolone 4 mg over counter order femara pills
purchase levoxyl online cheap vardenafil 10mg usa purchase levitra online
order biltricide 600mg for sale cyproheptadine 4mg for sale cyproheptadine 4 mg uk
buy lisinopril 5mg sale buy omeprazole no prescription buy lopressor online
buy lyrica 75mg pills pregabalin for sale buy priligy for sale
generic xenical 120mg order allopurinol 100mg without prescription zyloprim uk
buy cozaar 50mg without prescription topamax uk order topamax 100mg pills
crestor 10mg pills buy rosuvastatin 20mg without prescription buy domperidone pills
buy sumatriptan 25mg for sale purchase sumatriptan generic order avodart
buy toradol pill buy ketorolac pill buy generic inderal
order plavix 75mg without prescription order plavix 75mg for sale nizoral 200 mg pills
combivent 100 mcg cheap purchase ipratropium sale linezolid 600mg pills
generic simvastatin 10mg order viagra 50mg without prescription purchase viagra online cheap
duricef 250mg cost cefadroxil 500mg ca buy propecia 1mg
estradiol 2mg cost where can i buy estradiol how to buy prazosin
flagyl 200mg brand brand metronidazole 200mg keflex ca
order vermox online cheap buy retin gel sale buy tadalis cheap
avanafil 200mg drug voltaren 100mg pills buy voltaren generic
brand tamoxifen 20mg order ceftin 250mg online cefuroxime 500mg over the counter
generic indomethacin indocin order online order cefixime 200mg
careprost ca purchase careprost for sale trazodone order online
purchase amoxicillin generic buy generic arimidex online buy clarithromycin 500mg without prescription
buy clonidine no prescription buy spiriva 9 mcg without prescription tiotropium bromide buy online
generic minocycline hytrin order buy pioglitazone
arava where to buy arava 20mg oral purchase sulfasalazine sale
azithromycin 500mg sale buy gabapentin 600mg without prescription buy neurontin without a prescription
ivermectin cost canada best drug for ed purchase deltasone generic
buy lasix without prescription purchase vibra-tabs ventolin inhalator for sale
order levitra generic buy levitra paypal generic plaquenil 400mg
levitra 10mg usa hydroxychloroquine pills buy generic hydroxychloroquine 200mg
1998; McDonald et al priligy dapoxetine buy Zhao Ling had already told them not to Garden Herbs Lower Blood Pressure does losing weight lower high blood pressure disturb them, and neither of them dared to disobey Zhao Ling
order mesalamine for sale irbesartan 150mg brand cost irbesartan
clobetasol ca temovate where to buy cordarone tablet
order diamox online cheap imdur 40mg canada buy generic imuran for sale
lanoxin pills order molnupiravir 200mg without prescription molnupiravir 200 mg without prescription
naprosyn tablet order lansoprazole pills brand lansoprazole
buy albuterol medication buy albuterol no prescription order pyridium 200mg online cheap
norvasc 5mg over the counter prilosec to treat stomach order omeprazole 10mg online cheap
buy generic lopressor 100mg buy generic tenormin for sale medrol drug
oral dapoxetine misoprostol cost order generic xenical
buy diltiazem generic brand zovirax order generic zyloprim
oral rosuvastatin 10mg motilium oral buy motilium 10mg pills
buy ampicillin for sale buy acillin pills for sale flagyl 200mg usa
buy bactrim without prescription how to buy septra buy generic cleocin
cost erythromycin generic fildena tamoxifen 20mg pills
buy clopidogrel 150mg sale methotrexate 5mg usa purchase coumadin without prescription
robaxin 500mg cost buy generic robaxin 500mg suhagra uk
I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to know where u got this from. appreciate it
aurogra 50mg canada cost sildalis estrace 2mg oral
It was a ridiculous movie, Pitt scoffed is there a generic cialis available 1992; 13 477 481
celebrex 100mg pill cost tamsulosin 0.4mg purchase ondansetron generic
buy generic lamictal lamictal canada minipress 2mg canada
купить справку
I am in fact happy to read this blog posts which consists of plenty of helpful information, thanks for providing these data.
aldactone 25mg without prescription buy simvastatin 10mg pill how to buy valtrex
retin brand avana 100mg tablet order avana online
tadacip medication purchase indomethacin for sale buy indomethacin pills for sale
order tadalafil 20mg generic cialis sale buy ed meds
order terbinafine pill order amoxicillin online order generic trimox 500mg
Superb blog! Do you have any helpful hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally confused .. Any ideas? Appreciate it!
Currently it looks like Expression Engine is the best blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?
I think this is one of the such a lot significant information for me. And i’m satisfied reading your article. However wanna observation on few common things, The site taste is great, the articles is in reality nice : D. Just right task, cheers
how to buy anastrozole arimidex 1 mg cost catapres 0.1 mg us
generic azulfidine 500 mg calan 240mg cost calan online
What’s up to every body, it’s my first go to see of this webpage; this blog includes remarkable and actually good data in favor of readers.
If you wish for to get much from this article then you have to apply such techniques to your won website.
buy meclizine 25 mg without prescription buy meclizine medication order minocycline 100mg without prescription
order imuran 50mg sale oral telmisartan 80mg telmisartan tablet
buy erectile dysfunction drugs over the counter viagra 25mg price order viagra sale
generic movfor order naprosyn for sale order cefdinir 300 mg pills
Fantastic site. Lots of useful information here. I’m sending it to some buddies ans also sharing in delicious. And obviously, thank you on your effort!
Every weekend i used to pay a visit this web site, because i want enjoyment, as this this web site conations really pleasant funny stuff too.
lansoprazole for sale online buy proventil generic order protonix 20mg pill
It is perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or suggestions. Perhaps you could write next articles referring to this article. I wish to read more things about it!
how to buy pyridium order pyridium 200mg online cheap symmetrel cost
First off I want to say superb blog! I had a quick question that I’d like to ask if you don’t mind. I was curious to know how you center yourself and clear your mind before writing. I have had trouble clearing my mind in getting my thoughts out. I do enjoy writing but it just seems like the first 10 to 15 minutes are generally wasted just trying to figure out how to begin. Any suggestions or tips? Cheers!
I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to know where u got this from. thanks a lot
best ed medication cialis 5mg pill cialis coupon cvs
brand avlosulfon 100 mg order generic nifedipine 30mg buy cheap perindopril
It’s not my first time to go to see this website, i am visiting this web site dailly and get nice data from here daily.
Pretty component of content. I simply stumbled upon your web site and in accession capital to claim that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing in your augment or even I fulfillment you access consistently fast.
terazosin generic terazosin for sale price cialis
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is great blog. An excellent read. I’ll definitely be back.
buy amiodarone no prescription buy phenytoin paypal oral dilantin 100mg
purchase irbesartan generic temovate buspirone 5mg canada
Hi there, I found your web site via Google at the same time as searching for a similar topic, your site got here up, it seems to be good. I have bookmarked it in my google bookmarks.
Highly energetic blog, I liked that a lot. Will there be a part 2?
I am curious to find out what blog system you happen to be working with? I’m experiencing some minor security problems with my latest site and I would like to find something more safeguarded. Do you have any suggestions?
ditropan buy online cheap amitriptyline oral fosamax
I think what you postedtypedsaidbelieve what you postedwrotebelieve what you postedwrotethink what you postedwroteWhat you postedwrotesaid was very logicala lot of sense. But, what about this?think about this, what if you were to write a killer headlinetitle?content?typed a catchier title? I ain’t saying your content isn’t good.ain’t saying your content isn’t gooddon’t want to tell you how to run your blog, but what if you added a titlesomethingheadlinetitle that grabbed a person’s attention?maybe get people’s attention?want more? I mean %BLOG_TITLE% is a little vanilla. You could look at Yahoo’s home page and see how they createwrite post headlines to get viewers to click. You might add a related video or a pic or two to get readers interested about what you’ve written. Just my opinion, it might bring your postsblog a little livelier.
praziquantel 600 mg generic cost praziquantel periactin 4mg pill
Excellent article! We will be linking to this great post on our site. Keep up the good writing.
generic luvox 100mg fluvoxamine 100mg usa buy generic cymbalta over the counter
Hi there! This post couldn’t be written any better! Going through this post reminds me of my previous roommate! He always kept talking about this. I am going to forward this information to him. Pretty sure he will have a very good read. Many thanks for sharing!
I have been surfing online more than three hours nowadays, yet I never found any interesting article like yours. It’s lovely worth enough for me. In my opinion, if all site owners and bloggers made good content as you did, the internet might be much more useful than ever before.
After looking into a number of the blog posts on your web page, I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Please check out my web site as well and let me know what you think.
order anafranil 25mg generic prometrium 200mg brand oral prometrium 200mg
Having read this I thought it was extremely informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worth it!
tinidazole uk purchase tinidazole generic nebivolol 5mg price
brand rocaltrol buy cheap trandate cost fenofibrate
cheap diovan brand valsartan 80mg brand combivent 100mcg
An interesting discussion is worth comment. I do believe that you should write more on this issue, it might not be a taboo subject but generally people don’t discuss such topics. To the next! All the best!!
Thank you, I have recently been searching for information approximately this topic for ages and yours is the best I have found out so far. However, what about the conclusion? Are you positive concerning the source?
Woah! I’m really enjoying the template/theme of this site. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and visual appearance. I must say you have done a excellent job with this. Additionally, the blog loads very fast for me on Opera. Exceptional Blog!
buy dexamethasone sale zyvox 600mg for sale buy nateglinide online
Simply wish to say your article is as amazing. The clearness in your post is simply cool and i can assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please continue the rewarding work.
You can definitely see your enthusiasm in the article you write. The arena hopes for more passionate writers like you who aren’t afraid to mention how they believe. All the time go after your heart.
Heya! I’m at work browsing your blog from my new iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the great work!
bupropion medication generic zyrtec purchase strattera online cheap
buy capoten for sale buy tegretol without a prescription buy carbamazepine
This article will help the internet people for creating new blog or even a blog from start to end.
seroquel 50mg sale buy seroquel 100mg online cheap lexapro pills
ciprofloxacin 500 mg cost order lincomycin 500mg online cheap duricef 500mg pill
Excellent post. I’m going through some of these issues as well..
I really like it when individuals come together and share thoughts. Great blog, continue the good work!
Hi everyone, it’s my first visit at this website, and post is actually fruitful designed for me, keep up posting such articles.
We stumbled over here coming from a different page and thought I may as well check things out. I like what I see so now i’m following you. Look forward to going over your web page yet again.
buy generic frumil adapalene gel acyclovir over the counter
buy zebeta pills for sale cheap myambutol 600mg cheap oxytetracycline 250 mg
I’m not sure why but this web site is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later and see if the problem still exists.