www.example.com/custom-post-type/custom-taxonomy-name/post-name
我还希望类别元框仅在我们添加新的默认帖子时出现,而在我们将在自定义帖子类型和自定义分类框中添加一个新帖子,仅在我们以自定义帖子类型添加一个新帖子时出现,而在我们添加一个默认帖子时不显示。
#1 楼
首先,如果要仅将分类元数据框显示为自定义帖子类型,则通过将自定义帖子类型名称作为参数传递给register_taxonomy()
函数,将分类法仅注册到该自定义帖子类型。这样,分类法元框仅出现在自定义帖子类型中。如果您不想将类别metabox显示为自定义帖子类型,则在注册自定义帖子类型时删除术语类别作为自变量,而应包括如下分类标准名称:'taxonomies' => array( 'post_tag', 'your_taxonomy_name')
。这是我如何实现此目标的代码。我已经在自定义帖子类型主题下用“ themes_categories”子句注册了一个自定义分类法: -php prettyprint-override“>
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);
然后我用如下“ 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_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 );
现在,将自定义分类留为空白时。永久链接自动变为
http://www.example.com/wordpress/themes/other/post-name/
。最后,不要忘记单击WP后端管理部分的永久链接设置中的“保存更改”按钮来刷新重写,否则您将被重定向到404错误。希望对您有帮助。
#2 楼
即为自定义帖子类型注册自定义分类法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
}
}
评论
嘿,我遇到了问题...当我们使用echo get_the_term_list($ post-> ID,$ taxonomy,``,',','');输出到分类档案的链接时那么该链接显示为www.example.com/taxonomy-term而不是www.example.com/themes/taxonomy-term。我认为我们需要为此编写HTACESS规则。
– Saurabh Goel
2012年7月6日在13:49
+1,很好的解释,然后逐步进行,它可以在WordPress 3.4.2上测试
– Alex Vang
2012年10月23日19:27
我想知道:注册自定义帖子类型时,是否必须将自定义分类法添加到分类法数组中?因为它似乎也可以不添加而起作用(如果您已经在自定义帖子类型中注册了分类法)。只是好奇。
–火车头病
17 Mar 8 '17 at 7:52
使用CPT UI插件对此进行了尝试,同时仍然使用您的重写来更改URL。一切看起来不错。这些网址都是正确的,并且我重置了永久链接,但是实际的帖子引发了404。:(编辑:没关系。我仔细阅读并从分类法中删除了Hierarchical,并确保按正确的顺序保存内容,现在发布似乎有效。
– Garconis
18年5月17日在19:53