使用Uncode主题时如何显示WooCommerce商品分类描述
解决方案:Uncode主题商品分类描述显示优化
一、无需修改主题核心代码,通过过滤器开启描述显示
主题已预留过滤器uncode_use_description_in_product_tax_archives,直接添加以下代码到代码片段插件(或子主题的functions.php)即可启用描述显示,无需改动你找到的主题核心代码:
add_filter( 'uncode_use_description_in_product_tax_archives', '__return_true' );
这段代码会将过滤器默认值从false改为true,主题会优先显示分类描述(若分类已填写描述,无描述则回退显示分类标题)。
二、简化CSS编辑的优化方案
你之前遇到的CSS调整难题,大概率是因为描述与标题共用了.post-title类,样式冲突导致难以控制。以下两种方案可解决:
1. 自定义输出结构,给描述添加专属类
若需要更灵活的样式控制,可通过过滤器修改HTML结构,给描述单独设置类名:
add_filter( 'uncode_use_description_in_product_tax_archives', '__return_true' ); // 重写分类标题/描述的输出结构 add_filter( 'uncode_product_tax_archive_title', function( $title_content, $get_title ) { $is_description = get_queried_object()->description !== '' && apply_filters( 'uncode_use_description_in_product_tax_archives', false ); $text_class = $is_description ? 'post-category-description' : 'post-title'; return '<div class="post-title-wrapper"><h1 class="' . $text_class . '">' . $get_title . '</h1></div>'; }, 10, 2 );
此时显示分类描述时,h1会带有post-category-description类,显示标题时仍保留post-title类,你可针对专属类编写CSS:
/* 分类描述专属样式 */ .post-category-description { font-size: 1.2rem; color: #666; font-weight: 400; line-height: 1.6; margin-bottom: 2.5rem; } /* 原分类标题样式保留 */ .post-title { font-size: 2rem; color: #222; font-weight: 700; }
2. 直接通过页面标识编写CSS
若不想修改PHP结构,可利用WooCommerce分类归档页面的body类tax-product_cat做样式区分:
/* 分类归档页面的描述样式 */ .tax-product_cat .post-title { font-size: 1.2rem; color: #666; font-weight: normal; } /* 无描述时,保留原标题样式 */ .tax-product_cat .post-title:only-child { font-size: 2rem; color: #222; font-weight: bold; }
三、注意事项
- 禁止直接修改主题核心代码,否则主题更新后所有修改会丢失,使用子主题或代码片段插件添加代码更安全。
- 确保已在WooCommerce后台(产品→分类→编辑分类)填写对应分类的描述内容。
内容的提问来源于stack exchange,提问作者Caitlin
相关产品推荐
相关产品推荐

