Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将页面添加到wordpress网站,并通过自定义插件为其提供特定的短代码_Php_Wordpress_Wordpress Shortcode_Wordpress Plugin Creation - Fatal编程技术网

Php 如何将页面添加到wordpress网站,并通过自定义插件为其提供特定的短代码

Php 如何将页面添加到wordpress网站,并通过自定义插件为其提供特定的短代码,php,wordpress,wordpress-shortcode,wordpress-plugin-creation,Php,Wordpress,Wordpress Shortcode,Wordpress Plugin Creation,我已经做了一个动作,如果选中设置中的开关,就可以创建页面。在确认它被选中后,它确实创建了页面,但没有添加模板。如何使模板正常工作 add_action( 'admin_init', 'cart_page' ); function cart_page() { //Add cart page to website if ( get_option( 'cart' ) === "checked" AND get_option(

我已经做了一个动作,如果选中设置中的开关,就可以创建页面。在确认它被选中后,它确实创建了页面,但没有添加模板。如何使模板正常工作

        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_内容”中。我仍然想知道如何添加模板。但是,如果仍然需要,可以使用文件导入整个布局。但它不能与视觉作曲家一起使用

见下面的例子

 $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);

就我而言,我已经为这个简单的问题找到了解决方案。如果我只需要添加一个快捷码,我可以将其添加到“post_内容”中。我仍然想知道如何添加模板。但是,如果仍然需要,可以使用文件导入整个布局。但它不能与视觉作曲家一起使用

见下面的例子

 $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);