WooCommerce:重复类别子段,不同的父级

问题描述 投票:0回答:1

我们有一个 WooCommerce 网站,有 2 个父类别:

New Furniture
Used Furniture

它们都有一个名为

Chairs
的子类别。

这些是我们得到的网址:

/new-furniture/chairs

/used-furniture/chairs-1

我无法将

chairs-1
的 slug 更改为
chairs
,为什么会这样?我该如何解决这个问题?

php wordpress woocommerce
1个回答
0
投票

我最近试图解决这个问题,并用我的解决方案回答了类似的问题,并用匹配的答案弹出这个问题,因为这是一个更新的线程。

WordPress 不允许您在同一分类中存在重复的术语,它们在数据库中必须是唯一的。但是,我们可以扩展 WordPress 核心以使 url 结构正常工作,同时保留独特的术语 slugs。

使用您的示例:

new-furniture/chairs
used-furniture/chairs
网址,其中
new-furniture_chairs
used-furniture_charis
是唯一的子术语slugs,它们的父术语slugs分别是
new-furniture
used-furniture

首先,重写规则:

/**
 * Add custom rewrite rules for parent/child term urls.
 *
 * Adds a rewrite rule that will match terms 
 * that have their slug prefixed with the parent term
 * slug, separated by an underscore, but their url
 * is just the child term slug.
 */
function prefix_custom_hierarchical_taxonomy_rewrite_rules() {

    $tax_object = get_taxonomy( 'category' );

    if ( $tax_object ) {
        $tax_slug = $tax_object->rewrite['slug'];
        add_rewrite_rule(
            $tax_object->rewrite['slug'] . '/([^/]+)/([^/]+)/?',
            'index.php?' . $tax_object->query_var . '=$matches[1]_$matches[2]',
            'top'
        );
    }
}
add_action( 'init', 'prefix_custom_hierarchical_taxonomy_rewrite_rules', 100 );

然后过滤

term_link
,以便删除子术语链接中的 slug 的“parent_”部分:

/**
 * Filter the term link to handle hierarchical taxonomy permalinks.
 *
 * This will filter the term link to remove the parent term slug
 * from the child term url, so that the url is just the child term slug,
 * where the child term name is "parent_child" and the parent term name is "parent".
 *
 * @param string $link The term link
 * @param WP_Term $term The term object
 * @param string $taxonomy The taxonomy name
 * @return string
 */
function prefix_custom_term_link( $link, $term, $taxonomy ) {

    if ( $taxonomy !== 'category' ) {
        return $link;
    }

    // Get the parent term
    $parent_term = get_term( $term->parent, $taxonomy );

    if ( $parent_term && ! is_wp_error( $parent_term ) ) {
        // Remove the "parent_" prefix from the child term slug
        // to generate a url that looks like /parent/child/
        $link = str_replace( '/' . $term->slug, '/' . str_replace ( $parent_term->slug . '_', '', $term->slug ), $link );
    }

    return $link;
}
add_filter( 'term_link', 'prefix_custom_term_link', 10, 3 );

您可能还想过滤

parse_query
以检查是否有任何不遵循此结构的子术语。这避免了必须将 all 您的子类别 slug 设置为
parent_child
模式,您可以将其应用于那些具有“重复”url slug 的类别。

/**
 * Parse the query to handle hierarchical taxonomy permalinks.
 *
 * This filter the query to check for terms that are child terms
 * yet do not have the parent term in the slug, thus the parent/child
 * structure should literally look for a "child" slug, not "parent_child"
 *
 * @param WP_Query $query The WP_Query instance (passed by reference)
 * @return void
 */
function prefix_custom_parse_query($query) {
    if ( isset( $query->query_vars['category_name'] ) ) {
        $slug = $query->query_vars['category_name'];
        if ( strpos( $slug, '_' ) !== false ) {
            list( $parent, $child ) = explode( '_', $slug, 2 );
            if ( ! term_exists( $slug, 'category' ) && term_exists( $child, 'category' ) ) {
                $query->query_vars['category_name'] = $child;
            }
        }
    }
}
add_action( 'parse_query', 'prefix_custom_parse_query' );

我已将其概括为here

备注:

  • 这只适用于分层分类法,此示例专门针对
    category
    分类法。更新前两个片段中的分类法,并将第三个片段中的
    category_name
    查询变量替换为其他分类法的相应值。
  • prefix_
    命名更新为您的主题或插件独有的名称。
  • 此示例为您的子术语使用自定义术语别名,其遵循类似
    {parent-slug}_{child-slug}
    的模式。您可以将其更改为另一个分隔字符,但请小心使用破折号
    -
    ,因为这是生成名称时核心使用的默认值(空格变为破折号)。
  • 这需要“漂亮”的永久链接,其中
    /%postname%/
    在“设置”>“永久链接”中设置。
© www.soinside.com 2019 - 2024. All rights reserved.