我已经创建了“project”posttype和“project\\u category”texonomy。现在你要做的就是添加新项目,并添加你想在自定义posttype名称之前显示的项目类别,permalink意味着在2016类别中添加任何帖子。并将链接设置为自定义posttype“/%postname%/”。在函数中尝试此代码。php文件并检查。你也可以自定义posttypeI收到这个“www.abc”。com/2016/projects/test permalink/\'。
add_filter( \'pre_get_posts\', \'query_post_type\' );
function query_post_type( $query ) {
if ( is_category() ) {
$post_type = get_query_var( \'post_type\' );
if ( $post_type ) {
$post_type = $post_type;
} else { $post_type = array( \'nav_menu_item\', \'post\', \'projects\' ); // End if().
}
$query->set( \'post_type\',$post_type );
return $query;
}
}
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
\'name\' => _x( \'projects\', \'Post Type General Name\', \'namo\' ),
\'singular_name\' => _x( \'projects\', \'Post Type Singular Name\', \'namo\' ),
\'menu_name\' => __( \'projects\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent projects\', \'textdomain\' ),
\'all_items\' => __( \'All projects\', \'textdomain\' ),
\'view_item\' => __( \'View projects\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New projects\', \'textdomain\' ),
\'add_new\' => __( \'Add New\', \'textdomain\' ),
\'edit_item\' => __( \'Edit projects\', \'textdomain\' ),
\'update_item\' => __( \'Update projects\', \'textdomain\' ),
\'search_items\' => __( \'Search projects\', \'textdomain\' ),
\'not_found\' => __( \'Not Found\', \'textdomain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'textdomain\' ),
);
// Set other options for Custom Post Type
$args = array(
\'label\' => __( \'projects\', \'textdomain\' ),
\'description\' => __( \'projects news and reviews\', \'textdomain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 5,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
\'rewrite\' => array(
\'slug\' => \'%project_category%/projects\',
\'with_front\' => true,
),
// This is where we add taxonomies to our CPT
//\'taxonomies\' => array( \'category\' ),
);
// Registering your Custom Post Type
register_post_type( \'projects\', $args );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
\'name\' => _x( \'project_category\', \'taxonomy general name\', \'textdomain\' ),
\'singular_name\' => _x( \'project_category\', \'taxonomy singular name\', \'textdomain\' ),
\'search_items\' => __( \'Search project_categorys\', \'textdomain\' ),
\'popular_items\' => __( \'Popular project_categorys\', \'textdomain\' ),
\'all_items\' => __( \'All project_categorys\', \'textdomain\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit project_category\', \'textdomain\' ),
\'update_item\' => __( \'Update project_category\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New project_category\', \'textdomain\' ),
\'new_item_name\' => __( \'New project_category Name\', \'textdomain\' ),
\'separate_items_with_commas\' => __( \'Separate project_category with commas\', \'namo\' ),
\'add_or_remove_items\' => __( \'Add or remove project_category\', \'namo\' ),
\'choose_from_most_used\' => __( \'Choose from the most used project_category\', \'namo\' ),
\'not_found\' => __( \'No project_category found.\', \'namo\' ),
\'menu_name\' => __( \'project_category\', \'namo\' ),
);
$args1 = array(
\'hierarchical\' => false,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'update_count_callback\' => \'_update_post_term_count\',
\'query_var\' => true,
//\'rewrite\' => array( \'slug\' => \'project_category\' ),
\'rewrite\' => array(
\'slug\' => \'project_category\',
\'with_front\' => true,
),
);
register_taxonomy( \'project_category\', \'projects\', $args1 );
}
add_action( \'init\', \'custom_post_type\', 0 );
function so23698827_add_rewrite_rules( $rules ) {
$new = array();
$new[\'([^/]+)/(.+)/?$/projects\'] = \'index.php?projects=$matches[2]\';
$new[\'(.+)/?$/projects\'] = \'index.php?project_category=$matches[1]\';
return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( \'rewrite_rules_array\', \'so23698827_add_rewrite_rules\' );
function so23698827_filter_post_type_link( $link, $post ) {
if ( $post->post_type == \'projects\' ) {
if ( $cats = get_the_terms( $post->ID, \'project_category\' ) ) {
$link = str_replace( \'%project_category%\', current( $cats )->slug, $link );
}
}
return $link;
}
add_filter( \'post_type_link\', \'so23698827_filter_post_type_link\', 10, 2 );