微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义分类和帖子类型固定链接网址

如何解决自定义分类和帖子类型固定链接网址

注册自定义帖子类型和自定义分类法,旨在为我们的网站提供住房开发及其地块的层次结构,我们尝试实现的 URL 结构如下:

www.domain.com/developments/development-name/plot-name

然而,到目前为止我所做的似乎确实有效,但是永久链接已经从情节本身中消失并且是空的,所以我认为我做错了什么。我已经复制了下面的代码

function cptui_register_my_taxes() {

$labels = [
    "name" => __( "Developments","custom-post-type-ui" ),"singular_name" => __( "Development","menu_name" => __( "Developments","all_items" => __( "All Developments","edit_item" => __( "Edit Development","view_item" => __( "View Development","update_item" => __( "Update Development name","add_new_item" => __( "Add new Development","new_item_name" => __( "New Development name","parent_item" => __( "Parent Development","parent_item_colon" => __( "Parent Development:","search_items" => __( "Search Developments","popular_items" => __( "Popular Developments","separate_items_with_commas" => __( "Separate Developments with commas","add_or_remove_items" => __( "Add or remove Developments","choose_from_most_used" => __( "Choose from the most used Developments","not_found" => __( "No Developments found","no_terms" => __( "No Developments","items_list_navigation" => __( "Developments list navigation","items_list" => __( "Developments list","back_to_items" => __( "Back to Developments",];

$args = [
    "label" => __( "Developments","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","singular_name" => __( "Plot","menu_name" => __( "All Developments","all_items" => __( "All Plots","add_new" => __( "Add new","add_new_item" => __( "Add new Plot","edit_item" => __( "Edit Plot","new_item" => __( "New Plot","view_item" => __( "View Plot","view_items" => __( "View Plots","search_items" => __( "Search Plots","not_found" => __( "No Plots found","not_found_in_trash" => __( "No Plots found in bin","parent" => __( "Parent Plot:","featured_image" => __( "Featured image for this Plot","set_featured_image" => __( "Set featured image for this Plot","remove_featured_image" => __( "Remove featured image for this Plot","use_featured_image" => __( "Use as featured image for this Plot","archives" => __( "Plot archives","insert_into_item" => __( "Insert into Plot","uploaded_to_this_item" => __( "Upload to this Plot","filter_items_list" => __( "Filter Plots list","items_list_navigation" => __( "Plots list navigation","items_list" => __( "Plots list","attributes" => __( "Plots attributes","name_admin_bar" => __( "Plot","item_published" => __( "Plot published","item_published_privately" => __( "Plot published privately.","item_reverted_to_draft" => __( "Plot reverted to draft.","item_scheduled" => __( "Plot scheduled","item_updated" => __( "Plot updated.","parent_item_colon" => __( "Parent Plot:",];

$args = [
    "label" => __( "Plots","description" => "","rest_base" => "","rest_controller_class" => "WP_REST_Posts_Controller","has_archive" => 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 ],"menu_position" => 5,"menu_icon" => "dashicons-admin-home","supports" => [ "title","editor","thumbnail","custom-fields" ],"taxonomies" => [ "developments" ],];

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 );

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。