【发布时间】:2018-11-23 14:19:06
【问题描述】:
我有多个带有自定义分类法的自定义帖子类型。尽管有不同的父母,但我还是发生了冲突。
这是我的网址结构: /work/%client_name%/%project_name%
我有一个客户端 (client1) 和一个项目 (some-cool-project-name) 会生成这个 slug:“/work/client1/some-cool-project-name”。
当我在不同的父(客户端)下创建新帖子并为帖子提供相同的名称(和 slug)时,wordpress 会将 -2 附加到 slug:“/work/client2/some-cool-project-name -2"
自定义帖子类型为:
// Custom taxonomies.
function custom_taxonomies() {
$args = array(
'label' => __( 'Work', '' ),
'labels' => array(
'name' => __( 'Work', '' ),
'singular_name' => __( 'Work', '' ),
),
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => false,
'rest_base' => '',
'has_archive' => true,
'show_in_menu' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'work/%client_name%', 'with_front' => true ),
'query_var' => true,
'menu_icon' => 'dashicons-hammer',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' ),
'taxonomies' => array( 'client_name' ),
);
register_post_type( 'work', $args );
$args = array(
'label' => __( 'Clients', '' ),
'labels' => array(
'name' => __( 'Clients', '' ),
'singular_name' => __( 'Client', '' ),
),
'public' => true,
'hierarchical' => false,
'label' => 'Clients',
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'work/client_name', 'with_front' => false, ),
'show_admin_column' => false,
'show_in_rest' => false,
'rest_base' => '',
'show_in_quick_edit' => false,
);
register_taxonomy( 'client_name', array( 'work' ), $args );
}
add_action( 'init', 'custom_taxonomies' );
我的永久链接重写:
// Replace URL with proper taxonomy structure.
function permalink_rewrites( $link, $post ) {
if ( $post->post_status !== 'publish' || $post->post_type != 'work' ) {
return $link;
}
if ( $post->post_type == 'work' ) {
$type = '%client_name%/';
$filters = get_the_terms( $post->ID, 'client_name' );
$slug = $filters[0]->slug . '/';
}
if ( isset( $slug ) ) {
$link = str_replace( $type, $slug, $link );
}
return $link;
}
add_filter( 'post_type_link', 'permalink_rewrites', 10, 2 );
有什么建议可以解决这个问题吗?
谢谢。
【问题讨论】:
-
在 wordpress 中,任何帖子、分类、自定义帖子类型、页面等都必须有唯一的 slug,wordpress 绝不允许创建页面并使用相同的 slug 发布。
标签: wordpress url-rewriting custom-post-type slug custom-taxonomy