简体   繁体   中英

On my website I want to make a custom post type and several taxonomies (WordPress)

On my website I want to make a custom post type and several taxonomies. I have tried to code the following:

Post type: trabajo

if ( ! function_exists('set_custom_post') ) {

// Register Custom Post Type
function set_custom_post() {
    $rewrite = array(
        'slug'                  => 'trabajo',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => __( 'Trabajo', 'text_domain' ),
        'description'           => __( 'Trabajos realizados en Peces Gordos Studios S.L', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions', 'post-formats' ),
        'taxonomies'            => array( 'categoria_trabajo', 'etiqueta_trabajo' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-art',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => 'archivo-trabajos',
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'query_var'             => 'trabajo',
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
    );
    register_post_type( 'trabajo', $args );

}
add_action( 'init', 'set_custom_post', 0 );

}

Taxonomy categoria_trabajo:

if (  function_exists( 'categoria_trabajo' ) ) {

// Register Custom Taxonomy
function categoria_trabajo() {
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'query_var'                  => 'categoria',
        'rewrite'                    => $rewrite,
        'capabilities'               => $capabilities,
    );
    register_taxonomy( 'categoria_trabajo', array( 'trabajo' ), $args );

}
add_action( 'init', 'categoria_trabajo', 0 );

}

Taxonomy etiqueta_trabajo

if (  function_exists( 'etiqueta_trabajo' ) ) {

// Register Custom Taxonomy
function etiqueta_trabajo() {

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'query_var'                  => 'etiqueta',
        'rewrite'                    => $rewrite,
        'capabilities'               => $capabilities,
    );
    register_taxonomy( 'etiqueta_trabajo', array( 'trabajo' ), $args );

}
add_action( 'init', 'etiqueta_trabajo', 0 );

}

The custom post type appears in the administrator menu but the linked taxonomies do not appear.

I have compared the code with other source codes and I have not been able to find a solution.

I appreciate any help.

I noticed that in your code you're using $labels , $rewrite and $capabilities but these doesn't seem to be declared / initialized anywhere.

I tweaked your code a bit and this works for me:

/**
 * Trabajo CTP.
 */
function trabajo_post_type_init() {

    $args = array(
        'labels' => array(
            'name'               => 'Trabajos',
            'singular_name'      => 'Trabajo',
            'menu_name'          => 'Trabajos',
            'name_admin_bar'     => 'Trabajo',
            'add_new'            => 'Agregar',
            'add_new_item'       => 'Agregar nuevo trabajo',
            'new_item'           => 'Nuevo trabajo',
            'edit_item'          => 'Editar trabajo',
            'view_item'          => 'Ver trabajo',
            'all_items'          => 'Todos los trabajos',
            'search_items'       => 'Buscar trabajos',
            'parent_item_colon'  => 'Trabajos padre:',
            'not_found'          => 'No se encontraron trabajos',
            'not_found_in_trash' => 'No hay trabajos en la papelera'
        ),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => false,
        'rewrite' => array(
            'slug' => 'trabajos',
            'feeds' => false
        ),
        'query_var' => true,
        'menu_icon' => 'dashicons-art',
        'taxonomies' => array(
            'categoria_trabajo',
            'etiqueta_trabajo'
        ),
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',
            'post-formats'
        )
    );
    register_post_type( 'trabajo', $args );

}
add_action( 'init', 'trabajo_post_type_init' );

/**
 * Trabajo Taxonomies.
 */
function trabajo_taxonomies_init() {

    $args = array(
        'label'                         => 'Categorías',
        'labels'                        => array(
            'name'                          => 'Categorías',
            'singular_name'                 => 'Categoría',
            'search_items'                  => 'Buscar Categoría',
            'popular_items'                 => 'Categorías Populares',
            'all_items'                     => 'Todas las categorías',
            'parent_item'                   => 'Categoría padre',
            'edit_item'                     => 'Editar Categoría',
            'update_item'                   => 'Actualizar Categoría',
            'add_new_item'                  => 'Agregar Categoría',
            'new_item_name'                 => 'Nueva Categoría',
            'separate_items_with_commas'    => 'Separa las categorías con comas',
            'add_or_remove_items'           => 'Agregar o remover categorías',
            'choose_from_most_used'         => 'Elije de las categorías más utilizadas'
        ),
        'public'                        => true,
        'hierarchical'                  => true,
        'show_ui'                       => true,
        'show_in_nav_menus'             => true,
        'args'                          => array( 'orderby' => 'term_order' ),
        'query_var'                     => true
    );
    register_taxonomy( 'categoria_trabajo', 'trabajo', $args );

    $args = array(
        'label'                         => 'Etiquetas',
        'labels'                        => array(
            'name'                          => 'Etiquetas',
            'singular_name'                 => 'Etiqueta',
            'search_items'                  => 'Buscar Etiquetas',
            'popular_items'                 => 'Etiquetas Populares',
            'all_items'                     => 'Todas las etiquetas',
            'parent_item'                   => 'Etiqueta padre',
            'edit_item'                     => 'Editar Etiqueta',
            'update_item'                   => 'Actualizar Etiqueta',
            'add_new_item'                  => 'Agregar Etiqueta',
            'new_item_name'                 => 'Nueva Etiqueta',
            'separate_items_with_commas'    => 'Separa las etiquetas con comas',
            'add_or_remove_items'           => 'Agregar o remover etiquetas',
            'choose_from_most_used'         => 'Elije de las etiquetas más utilizadas'
        ),
        'public'                        => true,
        'hierarchical'                  => true,
        'show_ui'                       => true,
        'show_in_nav_menus'             => true,
        'args'                          => array( 'orderby' => 'term_order' ),
        'query_var'                     => true
    );
    register_taxonomy( 'etiqueta_trabajo', 'trabajo', $args );

}
add_action( 'init', 'trabajo_taxonomies_init' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM