我做了一个操作,如果检查了设置中的开关,就可以创建一个页面。在确认选中后,它确实会创建页面,但没有添加模板。如何使我的模板工作?
add_action( 'admin_init', 'cart_page' );
function cart_page() {
//Add cart page to website
if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
// IF CART HAS BEEN CHECKED
$new_page_id = wp_insert_post( array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
// Assign page template
'page_template' => plugins_url('page_templates/cart_template.php', __FILE__ )
) );
wp_insert_post($new_page_id);
update_option( 'cart_exist', true );
}
else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
// IF CUSTOMER DOES NOT WANT CART
update_option( 'cart_exist', false );
}
}
这就是我的模板页面在插件中的样子。
get_header();
echo do_shortcode( '[cart_portal]' );
get_footer();
?>
在我的案例中,我已经为这个简单的问题找到了解决方案。如果我只需要添加一个短代码,我可以将其添加到"post_content"中。我仍然想知道如何添加模板。但是,如果您仍然希望,可以使用文件导入整个布局。但它不能与视觉作曲家一起使用。
请参阅下面的示例。。。
$new_page_id = array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[cart_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
);
wp_insert_post($new_page_id);