特定于自定义邮政类型的自定义分类

时间:2012-07-05 作者:Saurabh Goel

我想创建一个自定义分类法,其行为类似于帖子类型,因为类别的行为类似于默认帖子(基于/%category%/%postname%/permalink结构),因此自定义帖子类型中的帖子显示为www.example.com/custom-post-type/custom-taxonomy-name/post-name此外,我希望类别元框出现only 当我们添加新的默认帖子而不是在自定义帖子类型中添加新帖子时,自定义分类框仅在我们在自定义帖子类型中添加新帖子时显示,而不是在我们添加新的默认帖子时显示。

2 个回复
最合适的回答,由SO网友:Rabin shrestha 整理而成

首先,如果希望仅向自定义帖子类型显示分类法元盒,则通过将自定义帖子类型名称作为参数传递给register_taxonomy() 作用通过执行此操作,分类法元盒仅显示为自定义帖子类型。如果不想向自定义帖子类型显示category metabox,请在注册自定义帖子类型时删除术语category作为参数,而是包括分类法段塞名称,如下所示:\'taxonomies\' => array( \'post_tag\', \'your_taxonomy_name\'). 这是我如何做到这一点的代码。

我已在custom post type themes下的slug“themes\\u categories”中注册了自定义分类法:

function themes_taxonomy() {
    register_taxonomy(
        \'themes_categories\',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
        \'themes\',             // post type name
        array(
            \'hierarchical\' => true,
            \'label\' => \'Themes store\', // display name
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'themes\',    // This controls the base slug that will display before each term
                \'with_front\' => false  // Don\'t display the category base before
            )
        )
    );
}
add_action( \'init\', \'themes_taxonomy\');
然后,为了更改永久链接,我创建了以下函数:

function filter_post_type_link( $link, $post ) {
    if ( $post->post_type !== \'themes\' )
        return $link;

    if ( $cats = get_the_terms($post->ID, \'themes_categories\') )
        $link = str_replace(\'%themes_categories%\', array_pop($cats)->slug, $link);

    return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
然后我注册了一个带有slug“themes”的自定义帖子类型,如下所示:

// Registering Custom Post Type Themes
add_action( \'init\', \'register_themepost\', 20 );
function register_themepost() {
    $labels = array(
        \'name\' => _x( \'Themes\', \'my_custom_post\',\'custom\' ),
        \'singular_name\' => _x( \'Theme\', \'my_custom_post\', \'custom\' ),
        \'add_new\' => _x( \'Add New\', \'my_custom_post\', \'custom\' ),
        \'add_new_item\' => _x( \'Add New ThemePost\', \'my_custom_post\', \'custom\' ),
        \'edit_item\' => _x( \'Edit ThemePost\', \'my_custom_post\', \'custom\' ),
        \'new_item\' => _x( \'New ThemePost\', \'my_custom_post\', \'custom\' ),
        \'view_item\' => _x( \'View ThemePost\', \'my_custom_post\', \'custom\' ),
        \'search_items\' => _x( \'Search ThemePosts\', \'my_custom_post\', \'custom\' ),
        \'not_found\' => _x( \'No ThemePosts found\', \'my_custom_post\', \'custom\' ),
        \'not_found_in_trash\' => _x( \'No ThemePosts found in Trash\', \'my_custom_post\', \'custom\' ),
        \'parent_item_colon\' => _x( \'Parent ThemePost:\', \'my_custom_post\', \'custom\' ),
        \'menu_name\' => _x( \'Themes Posts\', \'my_custom_post\', \'custom\' ),
    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'description\' => \'Custom Theme Posts\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'post-formats\', \'custom-fields\' ),
        \'taxonomies\' => array( \'post_tag\',\'themes_categories\'),
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 5,
        \'menu_icon\' => get_stylesheet_directory_uri() . \'/functions/panel/images/catchinternet-small.png\',
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array( \'slug\' => \'themes/%themes_categories%\', \'with_front\' => FALSE ),
        \'public\' => true,
        \'has_archive\' => \'themes\',
        \'capability_type\' => \'post\'
    );
    register_post_type( \'themes\', $args ); // max 20 character cannot contain capital letters and spaces
}
注册自定义帖子时,您需要记住的事情很少。将“has\\u archive”参数更改为自定义post类型slug名称,并将slug名称重写为\'slug\' => \'custom_post_type_slug/%taxonomy_slug%.

现在,当您在右侧的文章类型页面中添加新的文章类型时,您将看到永久链接http://www.example.com/wordpress/themes/%themes_categories%/post-name/. 如果未选择此帖子的自定义分类法,则永久链接将保留http://www.example.com/wordpress/themes/%themes_categories%/post-name/, 这将显示错误的请求。

为了纠正这一问题,我们在自定义分类法中创建了一个默认术语(与在类别中未分类相同)。

将此添加到函数。php:

function default_taxonomy_term( $post_id, $post ) {
    if ( \'publish\' === $post->post_status ) {
        $defaults = array(
            \'themes_categories\' => array(\'other\'),
        );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty($terms) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( \'save_post\', \'default_taxonomy_term\', 100, 2 );
现在,当自定义分类法留空时。永久链接变为http://www.example.com/wordpress/themes/other/post-name/ 自动地

最后,不要忘记通过单击WP backend admin部分的permalink设置中的“保存更改”按钮刷新重写,否则您将被重定向到404错误。我希望这对你有帮助。

SO网友:T.Todua

i、 e.注册自定义分类法MY_NEW_CARSS 对于自定义帖子类型:

$my_taxon_name  = \'MY_NEW_CARSS\';
$my_post_types  = array(\'SUB_CAT_1\',\'SUB_CAT_2\',\'SUB_CAT_3\');


//REGISTER CUSTOM TAXONOMY ( http://codex.wordpress.org/Function_Reference/register_taxonomy )
//If you aim to register HIERARCHICAL(Parent-ed) post type, read this warning: https://codex.wordpress.org/Function_Reference/register_post_type#hierarchical
add_action( \'init\', \'my_f32\' ); function my_f32() { 
    register_taxonomy( $GLOBALS[\'my_taxon_name\'], array(), 
        array( 
            \'label\'=>$GLOBALS[\'my_taxon_name\'],     \'public\'=>true, \'show_ui\'=>true,  \'show_admin_column\'=>true,   \'query_var\'=>true,
            \'hierarchical\'=>true,   \'rewrite\'=>array(\'with_front\'=>true,\'hierarchical\'=>true),  
             ));
}

//REGISTER CUSTOM POST TYPE ( http://codex.wordpress.org/Function_Reference/register_post_type )
add_action( \'init\', \'myf_63\' );function myf_63() { 

    foreach ($GLOBALS[\'my_post_types\'] as $each_Type)       {
            register_post_type( $each_Type, 
                array( 
                    \'label\'=>$each_Type,     \'labels\' => array(\'name\'=>$each_Type.\' pagess\', \'singular_name\'=>$each_Type.\' page\'),        \'public\' => true,   \'publicly_queryable\'=> true,      \'show_ui\'=>true,      \'capability_type\' => \'post\',      \'has_archive\' => true,      \'query_var\'=> true,     \'can_export\' => true,                   //\'exclude_from_search\' => false,     \'show_in_nav_menus\' => true,  \'show_in_menu\' => \'edit.php?post_type=page\',//true,     \'menu_position\' => 5,
                    \'hierarchical\' =>true,
                    \'supports\' =>array( \'page-attributes\', \'title\', \'editor\', \'thumbnail\' ), 
                    \'rewrite\' => array(\'with_front\'=>true, ),     //    \'rewrite\' => array("ep_mask"=>EP_PERMALINK ...) OR    \'permalink_epmask\'=>EP_PERMALINK, 
                ));

            register_taxonomy_for_object_type(\'category\',$each_Type);       //standard categories
            register_taxonomy_for_object_type($GLOBALS[\'my_taxon_name\'] ,$each_Type);   //Custom categories
    }
}

结束

相关推荐