我正在创建一个插件,我需要在其中创建一个动态页面并为其分配一个模板。我使用以下代码创建了它:
function gallerypage() {
global $wpdb;
$check_page = $wpdb->get_row( "SELECT * FROM wp_posts WHERE post_name = \'archive-gallery\'" );
if ( ! $check_page ) {
$page_time = the_time( \'Y-m-d g:i:s\' );
$gallery_page = array(
\'post_title\' => \'Archive Gallery\',
\'post_content\' => \'This page is dedicated to the archive gallery. and it is used to show all the gallerys\',
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'post_author\' => 1,
\'post_slug\' => \'archive-gallery\',
\'post_date\' => $page_time
);
//$wpdb->update($wpdb->prefix.\'pn_options\', array(\'option_value\'=>$post_id), array(\'id\'=>\'10\'));
$p_ID = wp_insert_post( $gallery_page );
update_post_meta( $p_ID, "_wp_page_template", "archivegallery.php" );
}
}
add_action( \'init\', \'gallerypage\' );
问题是,当我激活插件时,页面会一次又一次地创建,就像股市更新一样。我做错了什么?