WordPress:自定义分类法未显示在管理列中

时间:2019-09-25 08:20:33

标签: php wordpress

我不知道为什么自定义帖子类型的自定义分类法未显示在“管理”列中(它消失了)。

enter image description here

以下是始终有效的代码

SEW_SERVICE类 {

function __construct() {
    add_action( 'init',  array($this, 'init') );
    }

function init() {

    $this->taxonomies();
    $this->custom_posts();

}

function taxonomies() {

    $labels = array(
        'name'              => __( 'Categorie Servizi', SEWCL_TEXT_DOMAIN ),
        'singular_name'     => __( 'Categoria Servizio', SEWCL_TEXT_DOMAIN ),
        'search_items'      => __( 'Search Menu Sections', SEWCL_TEXT_DOMAIN ),
        'all_items'         => __( 'Tutte le categorie', SEWCL_TEXT_DOMAIN ),
        'parent_item'       => __( 'Categoria', SEWCL_TEXT_DOMAIN ),
        'parent_item_colon' => __( 'Categoria Servizio:', SEWCL_TEXT_DOMAIN ),
        'edit_item'         => __( 'Modifica Categoria Servizio', SEWCL_TEXT_DOMAIN ),
        'update_item'       => __( 'Aggiorna Categoria Servizio', SEWCL_TEXT_DOMAIN ),
        'add_new_item'      => __( 'Aggiungi Nuova Categoria', SEWCL_TEXT_DOMAIN ),
        'new_item_name'     => __( 'Categoria Servizio', SEWCL_TEXT_DOMAIN )
    );

    $args = array(
        'hierarchical'      => true, // Settare a false se invece di categorie si vogliono creare tag
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'sew-servizi' ),
    );

    register_taxonomy( 'sew-categorie-servizi', null, $args);

}



function custom_posts() {

    register_post_type( 'sew-servizio',
        array(
            'rewrite' => array( 'slug' => 'sew-servizio' ),
            'labels'  => array(
                'name'               => __('Lista Servizi', SEWCL_TEXT_DOMAIN),
                'singular_name'      => __('Servizo', SEWCL_TEXT_DOMAIN),
                'add_new'            => __('Aggiungi Servizo', SEWCL_TEXT_DOMAIN),
                'add_new_item'       => __('Aggiungi Nuovo Servizo', SEWCL_TEXT_DOMAIN),
                'edit'               => __('Modifica', SEWCL_TEXT_DOMAIN),
                'edit_item'          => __('Modifica Servizo', SEWCL_TEXT_DOMAIN),
                'new_item'           => __('Nuovo Servizo', SEWCL_TEXT_DOMAIN),
                'view'               => __('Visualizza', SEWCL_TEXT_DOMAIN),
                'view_item'          => __('Visualizza Servizo', SEWCL_TEXT_DOMAIN),
                'search_items'       => __('Cerca Servizo', SEWCL_TEXT_DOMAIN),
                'not_found'          => __('Nessuna servizio presente', SEWCL_TEXT_DOMAIN),
                'not_found_in_trash' => __('Nessun servizio presente nel cestino', SEWCL_TEXT_DOMAIN),
                'parent'             => __('Parent Movie Review', SEWCL_TEXT_DOMAIN)
            ),

            'public'              => true,
            'menu_position'       => 15,
            'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'page-attributes'  ),
            'taxonomies'          => array( 'sew-categorie-servizi' ),
            'menu_icon'           => 'dashicons-list-view',
            'has_archive'         => true,
            'exclude_from_search' => false,
            'show_in_nav_menus'   => false,
        )
    );

}

}

如此处所述:

https://codex.wordpress.org/Function_Reference/register_taxonomy

第二个参数设置为null,因为当我使用 register_post_type()

时,我将分类法与自定义帖子类型相关联

我不知道为什么代码停止工作。我使用了3年的这段代码。

分类法存在。如果我尝试通过URL访问它,那么它会起作用:

/wp-admin/edit-tags.php?taxonomy=sew-categorie-servizi&post_type=sew-servizio

enter image description here

似乎分类法未与自定义帖子相关联。实际上,在帖子的编辑屏幕中,将帖子与分类法关联的框甚至都不可见

有什么想法吗?

更新

我还尝试按时间顺序排列所有内容,但仍然无法正常工作

function custom_posts() {

        register_post_type( 'sew-servizio',
            array(
                'rewrite' => array( 'slug' => 'sew-servizio' ),
                'labels'  => array(
                    'name'               => __('Lista Servizi', SEWCL_TEXT_DOMAIN),
                    'singular_name'      => __('Servizo', SEWCL_TEXT_DOMAIN),
                    'add_new'            => __('Aggiungi Servizo', SEWCL_TEXT_DOMAIN),
                    'add_new_item'       => __('Aggiungi Nuovo Servizo', SEWCL_TEXT_DOMAIN),
                    'edit'               => __('Modifica', SEWCL_TEXT_DOMAIN),
                    'edit_item'          => __('Modifica Servizo', SEWCL_TEXT_DOMAIN),
                    'new_item'           => __('Nuovo Servizo', SEWCL_TEXT_DOMAIN),
                    'view'               => __('Visualizza', SEWCL_TEXT_DOMAIN),
                    'view_item'          => __('Visualizza Servizo', SEWCL_TEXT_DOMAIN),
                    'search_items'       => __('Cerca Servizo', SEWCL_TEXT_DOMAIN),
                    'not_found'          => __('Nessuna servizio presente', SEWCL_TEXT_DOMAIN),
                    'not_found_in_trash' => __('Nessun servizio presente nel cestino', SEWCL_TEXT_DOMAIN),
                    'parent'             => __('Parent Movie Review', SEWCL_TEXT_DOMAIN)
                ),

                'public'              => true,
                'menu_position'       => 15,
                'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'page-attributes'  ),
                //'taxonomies'          => array( 'sew-categorie-servizi' ),
                'menu_icon'           => 'dashicons-list-view',
                'has_archive'         => true,
                'exclude_from_search' => false,
                'show_in_nav_menus'   => false,
                /*
                'capabilities' => array(
                    'edit_post'          => 'update_core',
                    'delete_post'        => 'update_core',
                    'edit_posts'         => 'update_core',
                    'edit_others_posts'  => 'update_core',
                    'publish_posts'      => 'update_core',
                    'read_private_posts' => 'update_core'
                ),
                */
            )
        );

        $labels = array(
            'name'              => __( 'Categorie Servizi', SEWCL_TEXT_DOMAIN ),
            'singular_name'     => __( 'Categoria Servizio', SEWCL_TEXT_DOMAIN ),
            'search_items'      => __( 'Search Menu Sections', SEWCL_TEXT_DOMAIN ),
            'all_items'         => __( 'Tutte le categorie', SEWCL_TEXT_DOMAIN ),
            'parent_item'       => __( 'Categoria', SEWCL_TEXT_DOMAIN ),
            'parent_item_colon' => __( 'Categoria Servizio:', SEWCL_TEXT_DOMAIN ),
            'edit_item'         => __( 'Modifica Categoria Servizio', SEWCL_TEXT_DOMAIN ),
            'update_item'       => __( 'Aggiorna Categoria Servizio', SEWCL_TEXT_DOMAIN ),
            'add_new_item'      => __( 'Aggiungi Nuova Categoria', SEWCL_TEXT_DOMAIN ),
            'new_item_name'     => __( 'Categoria Servizio', SEWCL_TEXT_DOMAIN )
        );

        $args = array(
            'hierarchical'      => true, // Settare a false se invece di categorie si vogliono creare tag
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'sew-servizi' ), // Url /sew-servizi/nome-servizio
        );

        register_taxonomy( 'sew-categorie-servizi', array('sew-servizio'), $args);

    }

已解决

add_action('init',array($ this,'init'),PHP_MAX_INT);

3 个答案:

答案 0 :(得分:1)

您可以使用插件轻松控制自定义帖子和分类法 https://wordpress.org/plugins/custom-post-type-ui/

答案 1 :(得分:0)

事实上,我最喜欢的方法是使用cpt ui,如果尝试使用此代码,则非常方便

替换

 'show_in_nav_menus'   => false,

        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,

答案 2 :(得分:0)

解决了:

add_action( 'init',  array($this, 'init'), PHP_MAX_INT );