wordpress - 自定义帖子类型 Slug 冲突

标签 wordpress url-rewriting custom-post-type slug custom-taxonomy

我有多个带有自定义分类法的自定义帖子类型。尽管有不同的 parent ,但我还是发生了冲突。

这是我的网址结构:
/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 并不是真的这样设计的。即使在不同的类别中这对 2 个帖子/CPT 也不起作用的部分原因是,当一个在两个类别中时会发生什么?你必须开始获得一些讨厌的重写规则和 redirect_canonical() 涉及的功能 - 在这一点上,您只是要求 wazoo 出现 404 错误。

幸运的是,您可以做一些事情,而不是依赖具有相同 slug 的分类法和 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

您现在可以在此处看到此操作:
  • https://xhynk.com/content-mask/policies/rewrite-parent-a/rewrite-child/
  • https://xhynk.com/content-mask/policies/rewrite-parent-b/rewrite-child/

  • 这些设置完全按照我提到的方式进行:

    这样做的缺点是,如果您使用 /work/client-name要显示任何内容的页面,您现在必须设置一个 Post Type Template (自 WP 4.7.0 起可用)来实现您使用存档模板所具有的功能。

    然而,它阻止了重定向和重写规则的需要。

    这是永久链接结构的屏幕截图:
    Here's a screenshot of the permalink structure

    这是 CPT 概览管理页面的屏幕截图:
    Here's a screenshot of the CPT Overview admin page

    关于wordpress - 自定义帖子类型 Slug 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50846733/

    相关文章:

    wordpress - 在 odoo 中安装 woocommerce 连接器

    regex - 配置nginx语言子目录

    apache - 如何过滤请求以便 apache 处理它们而不是 tomcat?

    php - WordPress 按帖子类型对帖子进行排序

    html - LI 元素中 A 链接后的括号内内容的样式

    css - 如何从wordpress模板中删除发布日期

    mysql - Wordpress ACF 元值显示在管理和前端中,但不会显示在数据库中,直到我在管理中单击更新

    .htaccess - 使用 htaccess 重写规则的多域重定向

    php - 使用自定义分类法隐藏循环中的子帖子

    wordpress - 在wordpress中显示多个字段slug types_render_field