首先,如果希望仅向自定义帖子类型显示分类法元盒,则通过将自定义帖子类型名称作为参数传递给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错误。我希望这对你有帮助。