关于使用函数将分类添加到海关邮政类型的问题

时间:2013-11-13 作者:user2989953

使用WordPress 3.7.1和PHP 5.4.12,我试图将自定义帖子类型和分类添加到我的主题中,到目前为止,自定义帖子类型方法可以工作,但分类没有添加到管理仪表板中。

以下是我的代码:

<?php
 function add_post_type($name, $args = array()) {
    add_action(\'init\', function() use($name, $args) {
            $upper = ucwords($name);
            $name = strtolower(str_replace(\' \',\'_\',$name));
            $args = array_merge(
            array(
            \'public\'=> true,
            \'label\' => "All $upper" . \'s\',
            \'labels\' => array(\'add_new_item\' => "Add New $upper"),
            \'support\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\')
            ),
            $args
            );
            register_post_type(\'$name\', $args);
        });
}

function add_taxonomy($name, $post_type, $args = array()) {
    $name = strtolower($name);
    add_action(\'init\', function() use($name, $post_type, $args) {
            $args = array_merge(
                array(
                \'label\' => ucwords($name),
                ),
                $args
            );
                register_taxonomy($name, $post_type, $args);
    }); 
}

add_post_type(\'book\', array(
            \'supports\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\')
));
add_taxonomy(\'fun\', \'book\');
?>
你能告诉我我做错了什么吗?

2 个回复
SO网友:Nathan Powell

重复我的评论(CPT and Taxonomies connection), 您需要通过register_post_type 功能如下:

<?php
 function add_post_type($name, $args = array()) {
    add_action(\'init\', function() use($name, $args) {
            $upper = ucwords($name);
            $name = strtolower(str_replace(\' \',\'_\',$name));
            $args = array_merge(
            array(
            \'public\'=> true,
            \'label\' => "All $upper" . \'s\',
            \'labels\' => array(\'add_new_item\' => "Add New $upper"),
            \'support\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\'),
            //Add the taxonomy "fun" to this post type
            \'taxonomies\' => array(\'fun\'),
            ),
            $args
            );
            //Remove quotes from the $name variable
            register_post_type($name, $args);
        });
}


function add_taxonomy($name, $post_type, $args = array()) {
    $name = strtolower($name);
    add_action(\'init\', function() use($name, $post_type, $args) {
            $args = array_merge(
                array(
                \'label\' => ucwords($name),
                ),
                $args
            );
                register_taxonomy($name, $post_type, $args);
    }); 
}

add_post_type(\'book\', array(
            \'supports\' => array(\'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\')
));
add_taxonomy(\'fun\', \'book\');

SO网友:Ryan

这就是我用来为我的自定义帖子类型构建新的自定义分类法的方法。这对我很有用。

function fun_book() {
// Add new "Fun" taxonomy to Book Post Type
register_taxonomy(\'fun\', \'book\', array(
    // Hierarchical taxonomy (like categories)
    \'hierarchical\' => true,
    // This array of options controls the labels displayed in the WordPress Admin UI
    \'labels\' => array(
        \'name\' => _x( \'Fun\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'Fun\', \'taxonomy singular name\' ),
        \'search_items\' =>  __( \'Search Locations\' ),
        \'all_items\' => __( \'All Fun\' ),
        \'parent_item\' => __( \'Parent Fun\' ),
        \'parent_item_colon\' => __( \'Parent Fun:\' ),
        \'edit_item\' => __( \'Edit Fun\' ),
        \'update_item\' => __( \'Update Fun\' ),
        \'add_new_item\' => __( \'Add New Fun\' ),
        \'new_item_name\' => __( \'New Fun Name\' ),
        \'menu_name\' => __( \'Fun\' ),
    ),
    // Control the slugs used for this taxonomy
    \'rewrite\' => array(
        \'slug\' => \'Fun\', // This controls the base slug that will display before each term
        \'with_front\' => false, // Don\'t display the category base before "/locations/"
        \'hierarchical\' => true // This will allow URL\'s like "/locations/boston/cambridge/"
    ),
));
}
add_action( \'init\', \'fun_book\', 0 );
我真的希望这有帮助!

结束

相关推荐