Php 自定义分类法&;Post类型永久链接URL
我已经注册了一个自定义帖子类型和自定义分类法,旨在为我们的网站提供住房开发及其地块的层次结构,我们试图实现的URL结构如下: 然而,到目前为止,我所做的似乎是可行的,但是永久链接已经完成,从情节本身消失,并且是空的,所以我认为我做错了什么。我复制了下面的代码Php 自定义分类法&;Post类型永久链接URL,php,wordpress,function,custom-post-type,permalinks,Php,Wordpress,Function,Custom Post Type,Permalinks,我已经注册了一个自定义帖子类型和自定义分类法,旨在为我们的网站提供住房开发及其地块的层次结构,我们试图实现的URL结构如下: 然而,到目前为止,我所做的似乎是可行的,但是永久链接已经完成,从情节本身消失,并且是空的,所以我认为我做错了什么。我复制了下面的代码 function cptui_register_my_taxes() { $labels = [ "name" => __( "Developments", "custom
function cptui_register_my_taxes() {
$labels = [
"name" => __( "Developments", "custom-post-type-ui" ),
"singular_name" => __( "Development", "custom-post-type-ui" ),
"menu_name" => __( "Developments", "custom-post-type-ui" ),
"all_items" => __( "All Developments", "custom-post-type-ui" ),
"edit_item" => __( "Edit Development", "custom-post-type-ui" ),
"view_item" => __( "View Development", "custom-post-type-ui" ),
"update_item" => __( "Update Development name", "custom-post-type-ui" ),
"add_new_item" => __( "Add new Development", "custom-post-type-ui" ),
"new_item_name" => __( "New Development name", "custom-post-type-ui" ),
"parent_item" => __( "Parent Development", "custom-post-type-ui" ),
"parent_item_colon" => __( "Parent Development:", "custom-post-type-ui" ),
"search_items" => __( "Search Developments", "custom-post-type-ui" ),
"popular_items" => __( "Popular Developments", "custom-post-type-ui" ),
"separate_items_with_commas" => __( "Separate Developments with commas", "custom-post-type-ui" ),
"add_or_remove_items" => __( "Add or remove Developments", "custom-post-type-ui" ),
"choose_from_most_used" => __( "Choose from the most used Developments", "custom-post-type-ui" ),
"not_found" => __( "No Developments found", "custom-post-type-ui" ),
"no_terms" => __( "No Developments", "custom-post-type-ui" ),
"items_list_navigation" => __( "Developments list navigation", "custom-post-type-ui" ),
"items_list" => __( "Developments list", "custom-post-type-ui" ),
"back_to_items" => __( "Back to Developments", "custom-post-type-ui" ),
];
$args = [
"label" => __( "Developments", "custom-post-type-ui" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'developments', 'with_front' => false, ],
"show_admin_column" => false,
"show_in_rest" => true,
"rest_base" => "developments",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
"show_in_graphql" => false,
];
register_taxonomy( "developments", [ "plots" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes' );
function cptui_register_my_cpts() {
$labels = [
"name" => __( "Plots", "custom-post-type-ui" ),
"singular_name" => __( "Plot", "custom-post-type-ui" ),
"menu_name" => __( "All Developments", "custom-post-type-ui" ),
"all_items" => __( "All Plots", "custom-post-type-ui" ),
"add_new" => __( "Add new", "custom-post-type-ui" ),
"add_new_item" => __( "Add new Plot", "custom-post-type-ui" ),
"edit_item" => __( "Edit Plot", "custom-post-type-ui" ),
"new_item" => __( "New Plot", "custom-post-type-ui" ),
"view_item" => __( "View Plot", "custom-post-type-ui" ),
"view_items" => __( "View Plots", "custom-post-type-ui" ),
"search_items" => __( "Search Plots", "custom-post-type-ui" ),
"not_found" => __( "No Plots found", "custom-post-type-ui" ),
"not_found_in_trash" => __( "No Plots found in bin", "custom-post-type-ui" ),
"parent" => __( "Parent Plot:", "custom-post-type-ui" ),
"featured_image" => __( "Featured image for this Plot", "custom-post-type-ui" ),
"set_featured_image" => __( "Set featured image for this Plot", "custom-post-type-ui" ),
"remove_featured_image" => __( "Remove featured image for this Plot", "custom-post-type-ui" ),
"use_featured_image" => __( "Use as featured image for this Plot", "custom-post-type-ui" ),
"archives" => __( "Plot archives", "custom-post-type-ui" ),
"insert_into_item" => __( "Insert into Plot", "custom-post-type-ui" ),
"uploaded_to_this_item" => __( "Upload to this Plot", "custom-post-type-ui" ),
"filter_items_list" => __( "Filter Plots list", "custom-post-type-ui" ),
"items_list_navigation" => __( "Plots list navigation", "custom-post-type-ui" ),
"items_list" => __( "Plots list", "custom-post-type-ui" ),
"attributes" => __( "Plots attributes", "custom-post-type-ui" ),
"name_admin_bar" => __( "Plot", "custom-post-type-ui" ),
"item_published" => __( "Plot published", "custom-post-type-ui" ),
"item_published_privately" => __( "Plot published privately.", "custom-post-type-ui" ),
"item_reverted_to_draft" => __( "Plot reverted to draft.", "custom-post-type-ui" ),
"item_scheduled" => __( "Plot scheduled", "custom-post-type-ui" ),
"item_updated" => __( "Plot updated.", "custom-post-type-ui" ),
"parent_item_colon" => __( "Parent Plot:", "custom-post-type-ui" ),
];
$args = [
"label" => __( "Plots", "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "developments/%developments%", "with_front" => false ],
"query_var" => true,
"menu_position" => 5,
"menu_icon" => "dashicons-admin-home",
"supports" => [ "title", "editor", "thumbnail", "custom-fields" ],
"taxonomies" => [ "developments" ],
"show_in_graphql" => false,
];
register_post_type( "plots", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
function wpse_5308_post_type_link( $link, $post ) {
if ( $post->post_type === 'developments' ) {
if ( $terms = get_the_terms( $post->ID, 'developments' ) )
$link = str_replace( '%developments%', current( $terms )->slug, $link );
}
return $link;
}
add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );