具有特定类别结构的自定义帖子类型

时间:2014-08-16 作者:Roland

我已经成功创建了一个自定义帖子类型(又名“指令”)和两个不同的类别(又名“程序”和“练习”)。“指令”需要在“练习”下归档,练习需要将“程序”作为父类别。

以下是我注册分类法的方式:

    function create_taxonomy() {
        $programLabels = array(
            \'name\' => _x( \'Programs\', \'taxonomy general name\' ),
            \'singular_name\' =>_x( \'Program\', \'taxonomy singular name\' ),
            \'search_items\' =>  __( \'Search Programs\' ),
            \'popular_items\' => __( \'Popular Programs\' ),
            \'all_items\' => __( \'All Programs\' ),
            \'edit_item\' => __( \'Edit Program\' ),
            \'update_item\' => __( \'Update Program\' ),
            \'add_new_item\' => __( \'Add New Program\' ),
            \'new_item_name\' => __( \'New Program\' ),
            \'separate_items_with_commas\' => __( \'Separate programs with commas\' ),
            \'add_or_remove_items\' => __( \'Add or remove programs\' ),
            \'choose_from_most_used\' => __( \'Choose from the most used programs\' )
        );
        register_taxonomy(
            \'category_program\', // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
            \'cus_instruction\',      // post type name
            array(
                \'hierarchical\'      => false,
                \'label\'             => __(\'Program\'),
                \'labels\'            => $programLabels,
                \'query_var\'         => true,
                \'rewrite\'           => array(
                    \'slug\'          => \'program\',    // This controls the base slug that will display before each term
                    \'with_front\'    => false        // Don\'t display the category base before
                )
            )
        );

        $exerciseLabels = array(
            \'name\' => _x( \'Exercises\', \'taxonomy general name\' ),
            \'singular_name\' =>_x( \'Exercise\', \'taxonomy singular name\' ),
            \'search_items\' =>  __( \'Search Exercises\' ),
            \'popular_items\' => __( \'Popular Exercises\' ),
            \'all_items\' => __( \'All Exercises\' ),
            \'parent_item\' =>  __( \'Program\' ),
            \'parent_item_colon\' => ( \'Program:\' ),
            \'edit_item\' => __( \'Edit Exercise\' ),
            \'update_item\' => __( \'Update Exercise\' ),
            \'add_new_item\' => __( \'Add New Exercise\' ),
            \'new_item_name\' => __( \'New Exercise\' ),
            \'separate_items_with_commas\' => __( \'Separate exercises with commas\' ),
            \'add_or_remove_items\' => __( \'Add or remove exercise\' ),
            \'choose_from_most_used\' => __( \'Choose from the most used exercises\' ),
            \'menu_name\' => __( \'Exercises\' )
        );
        register_taxonomy(
            \'category_exercise\',   // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
            \'cus_instruction\',          // post type name
            array(
                \'hierarchical\'      => true,
                \'label\'             => __(\'Exercise\'),
                \'labels\'            => $exerciseLabels,
                \'query_var\'         => true,
                \'rewrite\'           => array(
                    \'slug\'          => \'exercise\',    // This controls the base slug that will display before each term
                    \'with_front\'    => false          // Don\'t display the category base before
                )
            )
        );
    }
    add_action( \'init\', \'create_taxonomy\', 0);
从概念上讲,将“cus\\u指令”设置为类别“Program”的post类型是不正确的,但这是我可以让Wordpress在管理侧栏中显示它的唯一方法。在“Exercise”类别中设置“parent\\u item”不会将类别“Program”绑定为父类别,但只会更改父类别的标签。

我的要求在Wordpress中是否可行?如果是,我如何设置类别结构?

1 个回复
SO网友:Rarst

我认为您已经找到了WP不太支持的结构。

让我们从以下前提开始:

分类法用于对帖子进行分组

在没有看到实际数据的情况下给出建议有点困难,但我的猜测是,如果程序/练习是分类法,并且需要有嵌套关系,那么它们可能只需要是一个层次分类法。

结束

相关推荐

GET Taxonomy ID

我有一行,其中“5”是分类ID。<?php echo function xyz (5,\'product_cat\'); ?>如何更改此项以使其始终自动识别当前页面的分类ID?尝试此操作但未成功:<?php echo function xyz (get_term_by(\'id\',\'\',\'product_cat);,\'product_cat\'); ?>我该怎么做?谢谢