动态创建自定义帖子类型项目并更新它们

时间:2011-03-02 作者:rexposadas

我正在创建一个使用自定义帖子类型的插件。我的问题有两个方面:

(1) 激活插件后,如何创建自定义帖子类型的项目。例如:如果我的帖子类型是say。。。“最佳餐厅”。我想创建10个自定义帖子类型项目,因为我的插件需要这些信息。我该怎么做?是否有一个函数可以用于添加post项及其自定义数据?

(2)当我的插件更新时,比如说我发布了一个新版本,修改这个列表的最佳方式是什么?我在考虑删除所有具有特定帖子类型的项目,然后插入新的项目,但这可能有点过头了。

提前谢谢。

2 个回复
最合适的回答,由SO网友:MikeSchinkel 整理而成

是的@wyrfel是对的,你使用wp_insert_post() 创建您的帖子。使用您的美国50个州的示例,我创建了一些代码,您可以将其放入主题的functions.php 查看其工作原理(尽管您可能不想打电话add_states_if_not_yet_added() 对于每个页面加载,但示例更容易以这种方式显示):

<?php 

add_action(\'init\',\'init_us_states\');
function init_us_states() {
  register_us_states_post_type();
  add_states_if_not_yet_added();
}
function add_states_if_not_yet_added() {
  foreach(get_50_us_states() as $state_code => $state_name) {
    if (!get_page_by_path($state_code,OBJECT,\'us-state\'))
      wp_insert_post(array(
        \'post_type\'       => "us-state",
        \'post_content\'    => "Information about {$state_name}",
        \'post_title\'      => $state_name, // i.e. \'Georgia\'
        \'post_name\'       => $state_code, // i.e. \'GA\'; this is for the URL
        \'post_status\'     => "publish",
        \'comment_status\'  => "closed",
        \'ping_status\'     => "closed",
        \'post_parent\'     => "0",
      ));
  }
}
function register_us_states_post_type() {
  register_post_type(\'us-state\',array(
      \'labels\' => array(
      \'name\' => _x(\'States\', \'post type general name\'),
      \'singular_name\' => _x(\'State\', \'post type singular name\'),
      \'add_new\' => _x(\'Add New\', \'us-state\'),
      \'add_new_item\' => __(\'Add New State\'),
      \'edit_item\' => __(\'Edit State\'),
      \'new_item\' => __(\'New State\'),
      \'view_item\' => __(\'View State\'),
      \'search_items\' => __(\'Search States\'),
      \'not_found\' =>  __(\'No States found\'),
      \'not_found_in_trash\' => __(\'No States found in Trash\'),
      \'parent_item_colon\' => \'\',
      \'menu_name\' => \'States\'
    ),
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(\'slug\'=>\'states\'),
    \'capability_type\' => \'post\',
    \'has_archive\' => \'states\',
    \'hierarchical\' => false,
    \'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'excerpt\')
  ));
}
function get_50_us_states() {
  return array(
    \'AL\' => \'Alabama\',
    \'AK\' => \'Alaska\',
    \'AZ\' => \'Arizona\',
    \'AR\' => \'Arkansas\',
    \'CA\' => \'California\',
    \'CO\' => \'Colorado\',
    \'CT\' => \'Connecticut\',
    \'DE\' => \'Delaware\',
    \'FL\' => \'Florida\',
    \'GA\' => \'Georgia\',
    \'HI\' => \'Hawaii\',
    \'ID\' => \'Idaho\',
    \'IL\' => \'Illinois\',
    \'IN\' => \'Indiana\',
    \'IA\' => \'Iowa\',
    \'KS\' => \'Kansas\',
    \'KY\' => \'Kentucky\',
    \'LA\' => \'Louisiana\',
    \'ME\' => \'Maine\',
    \'MD\' => \'Maryland\',
    \'MA\' => \'Massachusetts\',
    \'MI\' => \'Michigan\',
    \'MN\' => \'Minnesota\',
    \'MS\' => \'Mississippi\',
    \'MO\' => \'Missouri\',
    \'MT\' => \'Montana\',
    \'NE\' => \'Nebraska\',
    \'NV\' => \'Nevada\',
    \'NH\' => \'New Hampshire\',
    \'NJ\' => \'New Jersey\',
    \'NM\' => \'New Mexico\',
    \'NY\' => \'New York\',
    \'NC\' => \'North Carolina\',
    \'ND\' => \'North Dakota\',
    \'OH\' => \'Ohio\',
    \'OK\' => \'Oklahoma\',
    \'OR\' => \'Oregon\',
    \'PA\' => \'Pennsylvania\',
    \'RI\' => \'Rhode Island\',
    \'SC\' => \'South Carolina\',
    \'SD\' => \'South Dakota\',
    \'TN\' => \'Tennessee\',
    \'TX\' => \'Texas\',
    \'UT\' => \'Utah\',
    \'VT\' => \'Vermont\',
    \'VA\' => \'Virginia\',
    \'WA\' => \'Washington\',
    \'WV\' => \'West Virginia\',
    \'WI\' => \'Wisconsin\',
    \'WY\' => \'Wyoming\',
  );
}
下面是一些显示它正在使用的屏幕截图:

WordPress 3.1 post type page for a US StateList of US States on a WordPress 3.1 archive pageList of US State post types in the WordPress 3.1 admin

SO网友:wyrfel

对于(1):使用wp_insert_post().

对于(2):一个想法是如上所述插入帖子,并将插入ID存储在一个数组中。然后将该数组另存为选项。更新时,请阅读该选项并对这十篇您现在拥有ID的文章执行“wp\\u update\\u post()”。

结束

相关推荐