最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Keep the wordpress custom type, after disabling theme?

programmeradmin2浏览0评论

I used a theme, which created a custom type. Now I want to change the theme, but when I disable the theme, the custom type is also disabled. I tried using CPT plugin, and re-create the custom type, but it tells me that the custom type is already present and it can't recreate it.

I tried looking into the theme's function.php to copy the codes to my new child theme, but in the code it is refering to a lot of variables which I don't understand.

Can anyone help me please?

I used a theme, which created a custom type. Now I want to change the theme, but when I disable the theme, the custom type is also disabled. I tried using CPT plugin, and re-create the custom type, but it tells me that the custom type is already present and it can't recreate it.

I tried looking into the theme's function.php to copy the codes to my new child theme, but in the code it is refering to a lot of variables which I don't understand.

Can anyone help me please?

Share Improve this question asked Mar 6, 2020 at 12:59 Milad MoussaviMilad Moussavi 1
Add a comment  | 

3 Answers 3

Reset to default 0

You can use unregister_post_type('slug') - where 'slug' is the CPT, such as 'portfolio' or 'book' or whatever your particular CPT is, and then re-register the CPT. I would suggest doing this in your own custom plugin so you can change themes freely.

So if your CPT is 'portfolio' you can create a file in /wp-content/plugins/wpse_360145_create_cpt/create-cpt.php:

<?php
/* Plugin Name: Create CPT */

// Run everything when the plugin is activated
register_activation_hook(__FILE__, 'wpse_360145_activation');
function wpse_360145_activation() {
    // First, unregister the post type
    // (be sure to set this to *your* CPT)
    unregister_post_type('portfolio');
    // Next, re-register it from scratch
    // (You may have to play around with the settings)
    register_post_type('portfolio',
        array(
            // Plural, human-readable label for menu
            'label' => 'Portfolio Pieces',
            // Show in REST API must be true for the Block Editor
            'show_in_rest' => true,
            // Enable Title, Editor, and Excerpt
            'supports' => array('title', 'editor', 'excerpt'),
            // has_archive will create http://example/portfolio
            // much like a Post Category archive
            'has_archive' => 'portfolio'
        )
    );
}
?>

There are a number of other settings CPTs can have, so you may have to look up each parameter and adjust to make the CPT work exactly the way you want it to. See register_post_type() in the Code Reference.

With this approach, you just activate the plugin to try the settings, and if anything needs tweaking, deactivate, make the changes, and reactivate, and the unregister_post_type() call will make sure the old attempt is completely cleared out.

You can create a new folder in the plugins folder, called something like 'custom-taxonomy'. Create a file with the name 'custom-taxonomy.php' and paste the code below in it. The words 'Image' and 'Images' should be replaced with the name of the custom taxonomy of your current theme.

Don't forget to activate the plugin. Also, it could be that you'll need to change the true/false values for things like 'public' and 'has_archive'.

/**
 * Plugin Name: Custom Post Type
 * Plugin URI: http://www.mywebsite/my-first-plugin
 * Description: The very first plugin that I have ever created.
 * Version: 1.0
 * Author: Your Name
 * Author URI: http://www.mywebsite
 */    

function custom_post_type() {
        $post_names = array(
            'slug' => 'image',
            'singular' => __( 'Image', 'plugin-name' ),
            'plural'   => __( 'Images', 'plugin-name' )
        );

        register_post_type( strtolower($post_names['slug']),
            array(
                'labels'             => array(
                    'name'               => $post_names['plural'],
                    'singular_name'      => $post_names['singular'],
                    'add_new'            => sprintf( __( 'Add new %s', 'plugin-name' ), strtolower($post_names['singular']) ),
                    'add_new_item'       => sprintf( __( 'Add new %s', 'plugin-name' ), strtolower($post_names['singular']) ),
                    'edit'               => sprintf( __( 'Edit %s', 'plugin-name' ), strtolower($post_names['singular']) ),
                    'edit_item'          => sprintf( __( 'Edit %s', 'plugin-name' ), strtolower($post_names['singular'] )),
                    'new_item'           => sprintf( __( 'New %s', 'plugin-name' ), strtolower($post_names['singular'] )),
                    'all_items'          => sprintf( __( 'All %s', 'plugin-name' ), strtolower($post_names['plural'] )),
                    'view'               => sprintf( __( 'View %s', 'plugin-name' ), strtolower($post_names['singular'] )),
                    'view_item'          => sprintf( __( 'View %s', 'plugin-name' ), strtolower($post_names['singular'] )),
                    'search_items'       => sprintf( __( 'Search %s', 'plugin-name' ), strtolower($post_names['plural'] )),
                    'not_found'          => sprintf( __( 'No %s found', 'plugin-name' ), strtolower($post_names['plural']) ),
                    'not_found_in_trash' => sprintf( __( 'No %s found in trash', 'plugin-name' ), strtolower($post_names['plural'] )),
                    'parent_item_colon'  => '' /* text for parent types */
                ),
                'description'           => sprintf(__( 'Create an %s', 'plugin-name' ), strtolower($post_names['singular'])),
                'public'                => true,
                'show_ui'               => true,
                'show_in_menu'          => true,
                'publicly_queryable'    => true,
                /* queries can be performed on the front end */
                'has_archive'           => true,
                'rewrite'               => array(),
                'menu_position'         => 5,
                'show_in_nav_menus'     => true,
                'menu_icon'             => 'dashicons-images-alt2',
                'hierarchical'          => true,
                'query_var'             => true,
                'show_in_rest'          => true,
                /* Sets the query_var key for this post type. Default: true - set to $post_type */
                'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions', 'excerpt', 'page-attributes'),
            )
        );
    }
    add_action( 'init', 'custom_post_types', 11 );

In your old theme copy all of the code that registers the post "type register_post_type()" etc into the functions.php of the new theme.

发布评论

评论列表(0)

  1. 暂无评论