J.Z
J.Z

Reputation: 67

I created cpt in WP and did not show the taxonomy, what did I do wrong?

I created the Wordpress plugin below to show a CPT called articles within the admin panel.

Within this CPT I created a taxonomy item to register the categories.

But I'm having trouble creating the part that shows the CPT taxonomy.

Apparently the code part is correct but I do not find the problem.

I need help finding out what I wrote wrong in the code and why it does not show the taxonomy item in the CPT menu I created.

<?php
/*
Plugin Name: Artigos
Version: 1.0
*/

/*POST TYPE*/
add_action('init', 'cpt_artigos');
function cpt_artigos() { 

        $labels = array(
            'menu_name'          => __('Artigos'),
            'name'               => __('Artigos'),
            'singular_name'      => __('Artigo'),
            'all_items'          => __('Todos os cadastros'),
            'add_new'            => __('Adicionar novo cadastro', 'Novo cadastro'),
            'add_new_item'       => __('Novo cadastro'),
            'edit'               => __('Editar'),
            'edit_item'          => __('Editar cadastro'),
            'new_item'           => __('Novo cadastro'),
            'view'               => __('Ver'),
            'view_item'          => __('Ver cadastro'),
            'search_items'       => __('Procurar cadastros'),
            'not_found'          => __('Nenhum registro encontrado'),
            'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),      
         );

        $args = array(
            'labels' => $labels,            
            'menu_icon'           => 'dashicons-star-filled', 
            'capability_type'     => 'post', 
            'menu_position'       => 5, 
            'supports'            => array('title','editor','thumbnail'), 
            'public'              => true, 
            'publicly_queryable'  => true, 
            'exclude_from_search' => false, 
            'show_ui'             => true, 
            'show_in_nav_menus'   => true, 
            'show_in_menu'        => true, 
            'show_in_admin_bar'   => true, 
            'query_var'           => true, 
            'can_export'          => true, 
            'has_archive'         => true, 
            'hierarchical'        => true, 
            'rewrite'             => true 
         );

register_post_type( 'artigos' , $args ); 
flush_rewrite_rules();
}


//TAXONOMY
add_action( 'init', 'taxonomyartigos');
function taxonomyartigos() {

    $labels = array(
        'menu_name'         => __( 'Categorias de Artigos' ),
        'name'              => __( 'Categorias de Artigos' ),
        'singular_name'     => __( 'Categoria de Artigo' ),
        'search_items'      => __( 'Procurar Cadastros' ),
        'all_items'         => __( 'Todos os Cadastros' ),
        'parent_item'       => __( 'Categoria de Artigo Principal' ),
        'parent_item_colon' => __( 'Categoria de Artigo Principal:' ),
        'edit_item'         => __( 'Editar Cadastro' ), 
        'update_item'       => __( 'Atualizar Cadastro' ),
        'add_new_item'      => __( 'Adicionar novo cadastro' ),
        'new_item_name'     => __( 'Novo Cadastro' ),
     ); 

    $args = array(

        'labels'            => $labels,
        'public'            => true,
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'show_tagcloud'     => true,
        'show_in_nav_menus' => true,
        'has_archive'       => true,        
        'rewrite'           => true,
    );

register_taxonomy( 'taxonomyartigos', array( 'cpt_artigos' ), $args );
flush_rewrite_rules();
}


function cpt_artigos_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action('init', 'cpt_artigos_flush_rewrite');
?>

Upvotes: 1

Views: 73

Answers (2)

Md Masud
Md Masud

Reputation: 2711

Follow the documentation is a good idea in any case.

Below function takes 3 arguments in order.

  1. taxonomy string which will be registered

  2. post_type of CPT taxonomy will be attached with ( array or multiple CPT type or single CPT string)

  3. Taxomony args as an array.

    register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )
    

Ref: Here

Upvotes: 0

Tristup
Tristup

Reputation: 3663

You need to write the post type name instead of the function

register_taxonomy( 'taxonomyartigos', 'artigos', $args );

Hope it will do.

Upvotes: 2

Related Questions