ial*_*han 6 wordpress url-rewriting slug custom-post-type custom-taxonomy
我有多个带有自定义分类的自定义帖子类型.尽管有不同的父母,我仍然有一次slu cla冲突.
这是我的URL结构:/ 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' );
Run Code Online (Sandbox Code Playgroud)
我的固定链接重写:
// 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 );
Run Code Online (Sandbox Code Playgroud)
关于我能做什么的任何建议都可以解决这个问题?
谢谢.
不幸的是 WordPress 并不是真正这样设计的。即使在不同的类别中,这对于 2 个帖子/CPT 也不起作用,部分原因是当一个帖子/CPT 属于两个类别时会发生什么?你必须开始获取一些令人讨厌的重写规则和redirect_canonical()所涉及的函数——此时你只是要求出现 404 错误。
幸运的是,您可以做一些事情,而不是依赖相同的分类法和 CPT。您可以删除其中的分类部分并使用自定义帖子类型的分层格式。
这种方法有效的部分原因是您无法将多个家长分配给一个职位/CPT,因此不存在永久结构冲突。
创建一个名为 的新“工作” Client 1,第二个名为Client 2。
现在,创建这些“父作品”后,您可以创建第三个“作品”,名为Cool Project并将父项设置为Client 1,然后创建第四个“作品” Cool Project,名为 并将父项设置为Client 2。
这将为您提供以下永久链接结构:
https://example.com/work/client-1/cool-project
https://example.com/work/client-2/cool-project
Run Code Online (Sandbox Code Playgroud)
您现在可以在这里看到它的实际效果:
这些设置完全按照我提到的方式:
这样做的缺点是,如果您使用/work/client-name页面显示任何内容,您现在必须设置一个帖子类型模板(自 WP 4.7.0 起可用)来实现存档模板所具有的功能。
然而,它避免了重定向和重写规则的需要。
| 归档时间: |
|
| 查看次数: |
438 次 |
| 最近记录: |