我已经采取了一项措施,如果选中了设置中的开关,则可以创建页面。确认后,它会创建页面,但未添加模板。如何使我的模板正常工作?
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句