You need to enable JavaScript to run this app. 如何在WordPress分类编辑界面添加摘要替代分类描述?
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress分类编辑界面添加摘要替代分类描述?

需求实现可行性

这个需求完全可以落地,全程不需要修改WordPress核心程序文件,通过自定义代码即可完成,不会在WordPress版本更新时丢失修改,效果可完全匹配参考截图的展示位置与交互逻辑。

具体实现步骤

所有功能代码建议放置在子主题的functions.php文件中,或通过自定义代码类插件插入,避免直接修改父主题导致更新覆盖。

  1. 隐藏分类编辑页原有默认的Description描述输入项
    通过后台页面专属的钩子注入CSS,精准定位分类新增、编辑页面的原有描述字段容器,将其隐藏,不影响其他后台页面的功能:
    // 分类页面隐藏默认描述输入区块
    add_action('admin_head', 'hide_default_category_description');
    function hide_default_category_description() {
        $screen = get_current_screen();
        // 仅在分类法相关的后台页面加载这段样式
        if ( strpos( $screen->id, 'term' ) !== false ) {
            ?>
            <style>
                // 覆盖新增分类、编辑分类两个场景下的默认描述容器
                .term-description-wrap, .tag-description-wrap {
                    display: none !important;
                }
            </style>
            <?php
        }
    }
    
  2. 为分类页新增自定义的Excerpt摘要输入字段
    分三个逻辑模块完成字段的表单渲染、已存值读取、数据安全保存:
    // ① 新增分类页面挂载摘要输入框
    add_action( 'category_add_form_fields', 'render_category_excerpt_input' );
    function render_category_excerpt_input() {
        ?>
        <div class="form-field">
            <label for="category_excerpt">摘要(Excerpt)</label>
            <textarea name="category_excerpt" id="category_excerpt" rows="5" class="large-text"></textarea>
            <p class="description">请输入分类对应的短摘要,替代原有描述字段使用</p>
        </div>
        <?php
    }
    
    // ② 编辑已有分类页面挂载摘要输入框,同步读取已存的摘要值
    add_action( 'category_edit_form_fields', 'render_edit_category_excerpt_input', 10, 2 );
    function render_edit_category_excerpt_input( $term, $taxonomy ) {
        $saved_excerpt = get_term_meta( $term->term_id, 'category_excerpt', true );
        ?>
        <tr class="form-field">
            <th scope="row">
                <label for="category_excerpt">摘要(Excerpt)</label>
            </th>
            <td>
                <textarea name="category_excerpt" id="category_excerpt" rows="5" class="large-text"><?php echo esc_textarea( $saved_excerpt ); ?></textarea>
                <p class="description">请输入分类对应的短摘要,替代原有描述字段使用</p>
            </td>
        </tr>
        <?php
    }
    
    // ③ 保存/更新分类时,校验并存储摘要字段内容
    add_action( 'created_category', 'save_category_excerpt_data', 10, 3 );
    add_action( 'edited_category', 'save_category_excerpt_data', 10, 3 );
    function save_category_excerpt_data( $term_id, $tt_id, $taxonomy ) {
        if ( isset( $_POST['category_excerpt'] ) ) {
            update_term_meta(
                $term_id,
                'category_excerpt',
                sanitize_textarea_field( $_POST['category_excerpt'] )
            );
        }
    }
    
适配与注意事项
  • 若需要把这套功能扩展到文章标签、自定义分类法(比如WooCommerce产品分类等),只需要将代码中钩子的category替换为对应分类法的slug即可,逻辑完全复用。
  • 前端页面需要调用对应摘要时,通过get_term_meta( 当前分类ID, 'category_excerpt', true )即可读取存储的内容,不需要依赖原有的分类描述字段。
  • 原有隐藏的描述字段数据不会被删除,后续如果需要恢复显示,只要删除第一段注入CSS的代码即可完全还原原有功能,不会造成数据丢失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.24 21:45:04