通过代码在WordPress里插入各种数据

今天介绍的是一些通过代码编写,在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()函数。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 通过代码在WordPress里插入各种数据

165 评论

  1. sildenafil 100mg pills for sale tadalafil online tadalafil 20mg

  2. order spiriva 9 mcg pills spiriva 9 mcg usa buy terazosin 1mg generic

  3. purchase clobetasol online cheap buy buspar sale buy cordarone generic

  4. minocin over the counter oral minocycline order hytrin 5mg online

  5. stromectol for human online ed meds order deltasone 5mg online cheap

  6. buy zocor 10mg sale cheap phenergan purchase sildalis generic

  7. buy levaquin 500mg generic bupropion online bupropion online buy

  8. atorvastatin tablet viagra ca buy sildenafil 50mg for sale

  9. purchase oral ivermectin absorica online accutane 20mg cheap

  10. buy cozaar 50mg without prescription topamax uk order topamax 100mg pills

  11. generic minocycline hytrin order buy pioglitazone

  12. arava where to buy arava 20mg oral purchase sulfasalazine sale

  13. ivermectin cost canada best drug for ed purchase deltasone generic

  14. buy lasix without prescription purchase vibra-tabs ventolin inhalator for sale

  15. 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

  16. order diamox online cheap imdur 40mg canada buy generic imuran for sale

  17. buy diltiazem generic brand zovirax order generic zyloprim

  18. oral rosuvastatin 10mg motilium oral buy motilium 10mg pills

  19. buy bactrim without prescription how to buy septra buy generic cleocin

  20. buy clopidogrel 150mg sale methotrexate 5mg usa purchase coumadin without prescription

  21. 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

  22. aurogra 50mg canada cost sildalis estrace 2mg oral

  23. купить справку

  24. order tadalafil 20mg generic cialis sale buy ed meds

  25. 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!

  26. 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?

  27. 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

  28. 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.

  29. If you wish for to get much from this article then you have to apply such techniques to your won website.

  30. buy meclizine 25 mg without prescription buy meclizine medication order minocycline 100mg without prescription

  31. buy erectile dysfunction drugs over the counter viagra 25mg price order viagra sale

  32. 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!

  33. 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.

  34. 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!

  35. 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!

  36. 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

  37. 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.

  38. 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.

  39. 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.

  40. 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.

  41. Highly energetic blog, I liked that a lot. Will there be a part 2?

  42. 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?

  43. 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.

  44. 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!

  45. 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.

  46. 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.

  47. 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!

  48. 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!!

  49. 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?

  50. 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!

  51. 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.

  52. 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.

  53. bupropion medication generic zyrtec purchase strattera online cheap

  54. This article will help the internet people for creating new blog or even a blog from start to end.

  55. Excellent post. I’m going through some of these issues as well..

  56. I really like it when individuals come together and share thoughts. Great blog, continue the good work!

  57. Hi everyone, it’s my first visit at this website, and post is actually fruitful designed for me, keep up posting such articles.

  58. 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.

  59. buy generic frumil adapalene gel acyclovir over the counter

  60. 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.

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡