doukengsha9472 2018-02-14 16:07
浏览 63
已采纳

在我的网站上,我想制作一个自定义的帖子类型和几个分类法(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.

  • 写回答

1条回答 默认 最新

  • douxian4376 2018-02-14 22:24
    关注

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)