首页 > 解决方案 > 如何通过 wordpress 中的自定义插件创建自定义艺术家页面

问题描述

我正在研究更多并使用代码..我已经完成了这段代码。

if (!defined('ABSPATH')) {
    die;
}
if(!class_exists('ArtistPublic')):
class ArtistPublic
{
    public static function createTemplate( ) {

        global $user_ID;
            $new_post = array(
                'post_title' => 'Artist',
                'post_content' => 'hello Artist',
                'post_status' => 'publish',
                'post_date' => date('Y-m-d H:i:s'),
                'post_author' => $user_ID,
                'post_type' => 'page',
            );
            $post_id = wp_insert_post($new_post);
            if( !$post_id )
                wp_die('Error creating template page');
            else
                update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
        // Filter page template
//        add_filter('page_template', 'catch_plugin_template');


    }


    // Page template filter callback
    public static function catch_plugin_template($template) {
        if( is_page_template('artist.php') )
            $template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
        return $template;
    }
    }
endif;

但刷新后创建多个页面。有没有像 woocommerce 那样创建页面的好方法?

标签: wordpress

解决方案


所以代码就像什么

if (!defined('ABSPATH')) {
    die;
}

if(!class_exists('ArtistPublic')):
class ArtistPublic {
    public static function createTemplate( ) {    
        if( get_option('artist_page_id') ) :
            global $user_ID;
            $new_post = array(
                'post_title' => 'Artist',
                'post_content' => 'hello Artist',
                'post_status' => 'publish',
                'post_date' => date('Y-m-d H:i:s'),
                'post_author' => $user_ID,
                'post_type' => 'page',
            );
            $post_id = wp_insert_post($new_post);
            if( !$post_id ) :
                wp_die('Error creating template page');
            else :
                update_option('artist_page_id', $post_id);
                update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
            endif;
        endif;
        // Filter page template
//        add_filter('page_template', 'catch_plugin_template');
    }
    // Page template filter callback
    public static function catch_plugin_template($template) {
        if( is_page_template('artist.php') )
            $template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
        return $template;
    }
}
endif;

推荐阅读