是否在禁用主题后保留WordPress自定义类型?

时间:2020-03-06 作者:Milad Moussavi

我使用了一个主题,它创建了一个自定义类型。现在我想更改主题,但当我禁用主题时,自定义类型也被禁用。我尝试使用CPT插件,并重新创建自定义类型,但它告诉我自定义类型已经存在,无法重新创建。

我试着研究主题的功能。php将代码复制到我的新子主题,但在代码中它引用了许多我不理解的变量。

谁能帮帮我吗?

3 个回复
SO网友:Dominique Pijnenburg

您可以在插件文件夹中创建一个新文件夹,称为“自定义分类法”。创建名为“custom taxonomy”的文件。并将下面的代码粘贴到其中。单词“Image”和“Images”应替换为当前主题的自定义分类法的名称。

别忘了激活插件。此外,您可能需要更改“public”和“has\\u archive”等内容的真/假值。

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

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

SO网友:WebElaine

您可以使用unregister_post_type(\'slug\') - 其中,“slug”是CPT,如“投资组合”或“账簿”或您的特定CPT,然后重新注册CPT。我建议您在自己的自定义插件中执行此操作,以便您可以自由更改主题。

因此,如果您的CPT是“公文包”,您可以在/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.com/portfolio
            // much like a Post Category archive
            \'has_archive\' => \'portfolio\'
        )
    );
}
?>
CPT可以有许多其他设置,因此您可能必须查找每个参数并进行调整,以使CPT完全按照您希望的方式工作。看见register_post_type() in the Code Reference.

使用这种方法,您只需激活插件来尝试设置,如果需要调整,请停用、进行更改并重新激活,然后unregister_post_type() 呼叫将确保旧的尝试被完全清除。

SO网友:KiwisTasteGood

在旧主题中,将所有将post“type register\\u post\\u type()”等注册到函数中的代码复制到函数中。新主题的php。

相关推荐