我浏览了作为注释添加的两个URL,WordPress Codex文档,如register_taxonomy
here 还有一些,并想出了这个代码,似乎确实做到了这一点。主要添加的是添加的过滤器post_type_link
过滤cpt段塞并加载所需术语。
// Register Custom Taxonomy
function portfolio_taxonomy() {
$labels = array(
\'name\' => _x( \'Labels\', \'Taxonomy General Name\', \'imagewize_portfolio_plugin\' ),
\'singular_name\' => _x( \'Label\', \'Taxonomy Singular Name\', \'imagewize_portfolio_plugin\' ),
\'menu_name\' => __( \'Labels\', \'imagewize_portfolio_plugin\' ),
\'all_items\' => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
\'parent_item\' => __( \'Parent Item\', \'imagewize_portfolio_plugin\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
\'new_item_name\' => __( \'New Item Name\', \'imagewize_portfolio_plugin\' ),
\'add_new_item\' => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
\'edit_item\' => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
\'update_item\' => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
\'separate_items_with_commas\' => __( \'Separate items with commas\', \'imagewize_portfolio_plugin\' ),
\'search_items\' => __( \'Search Items\', \'imagewize_portfolio_plugin\' ),
\'add_or_remove_items\' => __( \'Add or remove items\', \'imagewize_portfolio_plugin\' ),
\'choose_from_most_used\' => __( \'Choose from the most used items\', \'imagewize_portfolio_plugin\' ),
\'not_found\' => __( \'Not Found\', \'imagewize_portfolio_plugin\' ),
);
$rewrite = array(
\'slug\' => \'label\',
\'with_front\' => true,
\'hierarchical\' => true,
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
\'rewrite\' => $rewrite,
);
register_taxonomy( \'label\', array( \'Img_portfolio_cpt\' ), $args );
}
// Hook into the \'init\' action
add_action( \'init\', \'portfolio_taxonomy\', 0 );
if ( ! function_exists(\'img_portfolio_cpt\') ) {
// Register Custom Post Type
function img_portfolio_cpt() {
$labels = array(
\'name\' => _x( \'Portfolio Items\', \'Post Type General Name\', \'imagewize_portfolio_plugin\' ),
\'singular_name\' => _x( \'Portfolio\', \'Post Type Singular Name\', \'imagewize_portfolio_plugin\' ),
\'menu_name\' => __( \'Portfolio\', \'imagewize_portfolio_plugin\' ),
\'parent_item_colon\' => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
\'all_items\' => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
\'view_item\' => __( \'View Item\', \'imagewize_portfolio_plugin\' ),
\'add_new_item\' => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
\'add_new\' => __( \'Add New\', \'imagewize_portfolio_plugin\' ),
\'edit_item\' => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
\'update_item\' => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
\'search_items\' => __( \'Search Item\', \'imagewize_portfolio_plugin\' ),
\'not_found\' => __( \'Not found\', \'imagewize_portfolio_plugin\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'imagewize_portfolio_plugin\' ),
);
$rewrite = array(
\'slug\' => \'%label%\',
\'with_front\' => true,
\'pages\' => true,
\'feeds\' => true,
);
$args = array(
\'label\' => __( \'Img_portfolio_cpt\', \'imagewize_portfolio_plugin\' ),
\'description\' => __( \'Imagewize Portfolio\', \'imagewize_portfolio_plugin\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', ),
\'taxonomies\' => array( \'label\' ),
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5,
\'menu_icon\' => \'dashicons-portfolio\',
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'page\',
);
register_post_type( \'Img_portfolio_cpt\', $args );
}
// Hook into the \'init\' action
add_action( \'init\', \'img_portfolio_cpt\', 0 );
}
add_filter(\'post_type_link\', \'portfolio_permalink_structure\', 10, 4);
function portfolio_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%label%\' ) ) {
$label_term = get_the_terms( $post->ID, \'label\' );
$post_link = str_replace( \'%label%\', array_pop( $label_term )->slug, $post_link );
}
return $post_link;
}
如@Pieter Goosen所述,必须检查CPT的命名问题。我还需要学习更多关于自定义帖子类型的知识,但今天确实学到了很多,并实现了目标。