当用户使用我的主题时,我希望我的主题在默认情况下有一个“关于”页面可用。
有没有一种方法可以在用户选择模板时自动创建页面?
或者其他实现这一点的方法?
当用户使用我的主题时,我希望我的主题在默认情况下有一个“关于”页面可用。
有没有一种方法可以在用户选择模板时自动创建页面?
或者其他实现这一点的方法?
有一个专门用于此目的的挂钩,称为after_switch_theme
. 你可以在主题激活后连接到它并创建你的个人页面。看看这个:
add_action( \'after_switch_theme\', \'create_page_on_theme_activation\' );
function create_page_on_theme_activation(){
// Set the title, template, etc
$new_page_title = __(\'About Us\',\'text-domain\'); // Page\'s title
$new_page_content = \'\'; // Content goes here
$new_page_template = \'page-custom-page.php\'; // The template to use for the page
$page_check = get_page_by_title($new_page_title); // Check if the page already exists
// Store the above data in an array
$new_page = array(
\'post_type\' => \'page\',
\'post_title\' => $new_page_title,
\'post_content\' => $new_page_content,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_name\' => \'about-us\'
);
// If the page doesn\'t already exist, create it
if(!isset($page_check->ID)){
$new_page_id = wp_insert_post($new_page);
if(!empty($new_page_template)){
update_post_meta($new_page_id, \'_wp_page_template\', $new_page_template);
}
}
}
我建议您使用一个独特的slug来确保代码始终正常运行,因为带有slug的页面about-us
非常常见,可能已经存在,但不属于您的主题。wp\\u insert\\u post()函数是从何处开始的https://developer.wordpress.org/reference/functions/wp_insert_post/ . 您可以将创建帖子的代码放在主题激活代码中。谷歌展示了许多使用该功能的示例。
(虽然我不确定主题用户是否会希望自动创建帖子。在我看来,这对你来说有点过分。我肯定不希望在我的网站上自动创建帖子。即使在主题激活之前进行了充分披露。有点像“不伤害”别人的博客。)
Starter内容是一种用户友好的方式,因此不会对用户强加任何不需要的内容。看看https://make.wordpress.org/core/2016/11/30/starter-content-for-themes-in-4-7/
add_action(\'wpmu_new_blog\', \'create_pages\');
function create_pages(){
$awesome_page_id = get_option("awesome_page_id");
if (!$awesome_page_id) {
//create a new page and automatically assign the page template
$post1 = array(
\'post_title\' => "Awesome Page!",
\'post_content\' => "",
\'post_status\' => "publish",
\'post_type\' => \'page\',
);
$postID = wp_insert_post($post1, $error);
update_post_meta($postID, "_wp_page_template", "awesome-page.php");
update_option("awesome_page_id", $postID);
}
}
我正试图建立一个多供应商联盟网站。我想知道如何在用户登录时识别用户类型(如供应商、客户),以便我可以隐藏一些页面,例如供应商仪表板。