是的@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\',
);
}
下面是一些显示它正在使用的屏幕截图: