我想创建一个自定义分类法,其行为类似于帖子类型,因为类别的行为类似于默认帖子(基于/%category%/%postname%/永久链接结构),因此自定义帖子类型中的帖子为显示为 www.example.com/custom-post-type/custom-taxonomy-name/post-name 我也希望类别元框仅在我们添加新的默认帖子时出现,而不是在自定义中添加新帖子时出现帖子类型和自定义分类框仅在我们在自定义帖子类型中添加新帖子时出现,而在我们添加默认帖子时不出现。
我想创建一个自定义分类法,其行为类似于帖子类型,因为类别的行为类似于默认帖子(基于/%category%/%postname%/永久链接结构),因此自定义帖子类型中的帖子为显示为 www.example.com/custom-post-type/custom-taxonomy-name/post-name 我也希望类别元框仅在我们添加新的默认帖子时出现,而不是在自定义中添加新帖子时出现帖子类型和自定义分类框仅在我们在自定义帖子类型中添加新帖子时出现,而在我们添加默认帖子时不出现。
Answers:
首先,如果您只想将分类元数据框显示为自定义帖子类型,则通过将自定义帖子类型名称作为参数传递给register_taxonomy来将分类法仅注册到该自定义帖子类型。这样,分类法元框仅出现在自定义帖子类型中。如果您不想将类别metabox显示为自定义帖子类型,则在注册自定义帖子类型时删除术语类别作为参数,而应包含分类标段名称,例如“ taxonomies” => array('post_tag','your_taxonomy_name') 。这是我如何实现的代码。我已经在自定义帖子类型主题下的子主题themes_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);
然后我用如下主题注册了一个自定义帖子类型
//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 charachter cannot contain capital letters and spaces
}
注册自定义帖子时,您需要记住的事情很少。将has_archive参数更改为自定义帖子类型子弹名称,另一个是将重写子弹名称更改为'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/,这将显示错误的请求。为了解决这个问题,我们在自定义分类法中创建一个默认术语。(与未分类的类别相同)将其添加到functions.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 );
现在,当自定义分类法留为空白时,permlaink会 自动变为 http://www.example.com/wordpress/themes/other/post-name/。
最后,不要忘记单击“管理”部分中“永久链接”设置中的“保存更改”来刷新重写,否则您将被重定向到404错误。希望这对您有所帮助。
即为自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
}
}