You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为WooCommerce创建自定义分类及URL结构配置咨询

解决方案

问题1:创建/services/和/services/category/归档页面

先修正你代码里的分类slug:当前rewrite参数里的slug是ser/category,改成services/category,才能匹配你想要的URL格式。

1. 实现/services/(服务产品归档)

如果服务类是独立的自定义文章类型(比如你用CPT UI创建的services),创建时勾选「启用归档」,WordPress会自动生成example.com/services/归档页,你可以用主题目录下的archive-services.php模板自定义样式。

如果服务类是基于WooCommerce的product类型,靠ser分类区分,要让example.com/services/显示所有该分类下的产品,按以下步骤来:

  • 在主题目录新建page-services-archive.php模板文件,写入以下代码:
<?php
/*
Template Name: 服务产品归档
*/
get_header();

// 查询所有属于ser分类的产品
$args = array(
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'ser',
            'field'    => 'slug',
            'terms'    => get_terms('ser', array('fields' => 'slugs')),
            'operator' => 'IN'
        )
    )
);
$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        // 调用WooCommerce产品模板展示
        wc_get_template_part('content', 'product');
    endwhile;
    wp_reset_postdata();
else :
    echo '<p>暂无服务产品</p>';
endif;

get_footer();
?>
  • 后台新建页面,标题设为「服务」,固定链接slug设为services,页面模板选择刚才创建的「服务产品归档」,发布后example.com/services/就会正常显示归档内容。

2. 实现/services/category/(服务分类列表)

默认WordPress不会自动生成分类术语的列表页,需要自定义:

  • 在主题目录新建page-services-category.php模板文件,写入以下代码:
<?php
/*
Template Name: 服务分类归档
*/
get_header();

// 查询所有ser分类的术语
$terms = get_terms(array(
    'taxonomy' => 'ser',
    'hide_empty' => true,
));

if (!empty($terms)) :
    echo '<ul class="service-categories">';
    foreach ($terms as $term) :
        echo '<li><a href="' . get_term_link($term) . '">' . $term->name . '</a></li>';
    endforeach;
    echo '</ul>';
else :
    echo '<p>暂无服务分类</p>';
endif;

get_footer();
?>
  • 后台新建页面,固定链接slug设为services/category,页面模板选择「服务分类归档」,发布后example.com/services/category/就会显示所有服务分类的列表。

问题2:修改服务类产品URL为example.com/services/product-name

要把ser分类下的产品URL从shop/product-name改成services/product-name,需要添加两个功能:修改permalink输出,以及添加rewrite规则让WordPress识别新URL。

在主题的functions.php或者自定义插件里添加以下代码:

// 修改产品permalink,替换shop为services
function custom_service_product_permalink($permalink, $post) {
    if ($post->post_type === 'product' && has_term('', 'ser', $post)) {
        $permalink = str_replace('/shop/', '/services/', $permalink);
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_service_product_permalink', 10, 2);

// 添加rewrite规则,让WordPress解析services开头的产品URL
function custom_service_product_rewrite_rule() {
    add_rewrite_rule(
        'services/([^/]+)/?$',
        'index.php?product=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_service_product_rewrite_rule');

添加完成后,进入后台「设置-固定链接」页面,点击「保存更改」刷新rewrite规则,之后ser分类下的产品URL就会变成example.com/services/product-name。

如果其他三类产品也需要类似的URL结构,复制上述代码,替换ser分类标识和对应的前缀(比如physical、website、courses)即可。


内容的提问来源于stack exchange,提问作者widkoinflm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:40:23