对不起,我的英语不好。
我想在激活插件时创建多个页面。这也是可行的,但是如何在不同的页面中获得不同的内容呢?
if (!current_user_can(\'activate_plugins\')) return;
$page_titles = array(
\'Login\',
\'Dashboard\'
);
foreach($page_titles as $page_title) {
$page = get_page_by_title( $page_title );
if( ! isset ( $page ) ) {
// create post object
$my_post = array(
\'post_title\' => $page_title,
\'post_content\' => \'[shortcode]\',
\'post_status\' => \'publish\',
\'post_author\' => get_current_user_id(),
\'post_type\' => \'page\',
);
// insert the post into the database
wp_insert_post($my_post);
}
}
我很高兴能得到帮助
SO网友:fuxia
您只需将内容添加到初始数组中。最简单的方法是使用标题作为数组索引,如下所示:
$pages = [
\'Login\' => \'Some content\',
\'Dashboard\' => \'Some other content\'
];
foreach ( $pages as $title => $content )
{
if ( get_page_by_title( $title ) ) {
continue; // skip this page
}
wp_insert_post([
\'post_title\' => $title,
\'post_content\' => $content,
\'post_status\' => \'publish\',
\'post_author\' => get_current_user_id(),
\'post_type\' => \'page\',
]);
}