Php Wordpress自定义帖子类型重写页面和类别slug
我已经设置了自定义的帖子类型Php Wordpress自定义帖子类型重写页面和类别slug,php,wordpress,url-rewriting,custom-post-type,Php,Wordpress,Url Rewriting,Custom Post Type,我已经设置了自定义的帖子类型 register_post_type( 'Communities', array( 'labels' => array( 'name' => __( 'Communities' ), 'singular_name' => __( 'Community' ), 'not_found' => __( 'No Communities Found' ) ),
register_post_type( 'Communities',
array(
'labels' => array(
'name' => __( 'Communities' ),
'singular_name' => __( 'Community' ),
'not_found' => __( 'No Communities Found' )
),
'public' => true,
'rewrite' => array( 'slug' => '/%category%/communities', 'with_front' => false),
'has_archive' => true,
'hierarchical' => true,
'taxonomies' => array( 'category' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
'menu_icon' => 'dashicons-admin-home'
)
);
我有一个带有城市名称的页面设置,出于不同的原因,我需要将其保留为页面,但我希望该城市中的社区存储为自定义帖子类型。我已经为我要添加社区的每个城市设置了类别,但我希望他们将cityname页面视为父级
像这样
www.sitename.com/cityname/communities/communityname/
我希望cityname是一个页面,communities是一个归档页面,communityname是一个单独的页面
到目前为止,我找到的每个解决方案都会与父页面产生404个错误或冲突
感谢您的帮助。谢谢 这应该能帮到你,但首先有两件事你应该知道 1.)我不推荐这种URL结构,因为它要求每个条目至少有一个类别。它将始终抓住阵列中的第一个。你需要写一个小插件让你的作者知道。这可能会起到以下作用: 2.)在查看帖子之前,您需要在添加代码后访问Settings>permalinks,以便强制重写刷新
谢谢你迄今为止的帮助。我使用了上面的代码,并在我的自定义主题和Twenty16中对其进行了测试。它适用于归档和单页,但是父页显示为归档。另外,其他页面(如联系人页面等)正在生成404个错误。看起来它几乎就要出现了。。。你有什么建议我可以试试吗?这解决了404的问题,但我仍然认为结构可以设置得更干净一些。可能是自定义分类法,而不是使用post的分类法。然后,您可以使用ACF建立关系。请参阅下面的更新,因为它将向您展示如何重新编写您认为合适的规则。再次感谢。是的,我想你是对的。。设置自定义分类法我们可能是最好的选择。我注册了自定义分类法,从仪表板上看url结构似乎很好。但是,当您单击链接查看社区时,它仍然会生成404。
/**
* Register Community Custom Post Type
*/
function community_post_type() {
$labels = array(
'name' => 'Communities',
'singular_name' => 'Community',
'menu_name' => 'Communities',
'name_admin_bar' => 'Communities',
'archives' => 'Community Archives',
'attributes' => 'Community Attributes',
'parent_item_colon' => 'Parent Community:',
'all_items' => 'All Communities',
'add_new_item' => 'Add New Community',
'add_new' => 'Add New',
'new_item' => 'New Community',
'edit_item' => 'Edit Community',
'update_item' => 'Update Community',
'view_item' => 'View Community',
'view_items' => 'View Communities',
'search_items' => 'Search Communities',
'not_found' => 'Community Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into item',
'uploaded_to_this_item' => 'Uploaded to this Community',
'items_list' => 'Communities list',
'items_list_navigation' => 'Communities list navigation',
'filter_items_list' => 'Filter Communities list',
);
$rewrite = array(
'slug' => '/%category%/communities',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => 'Community',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
'taxonomies' => array( 'category' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-universal-access',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_in_rest' => true,
);
register_post_type( 'communities', $args );
}
add_action( 'init', 'community_post_type', 0 );
/**
* Add category slug to community post links
* @param $post_link
* @param $id
*/
function my_commmunity_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) && $post->post_type == 'communities' ){
$terms = wp_get_object_terms( $post->ID, 'category' );
if( !empty($terms) ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'my_commmunity_post_link', 1, 3 );