如何为WordPress分类添加选择不同模板选项(代码版)

本文介绍了为WordPress不同分类设置不同展示样式的简便方法。传统方式需创建多个分类模板文件或使用判断函数,操作繁琐。推荐安装Custom Category Templates插件,或在主题functions.php中添加相应代码,即可在编辑分类时直接选择专用模板,实现个性化布局。

WordPress基础教程
阅读时间: 33 分钟
最后更新时间:2025年11月13日

我们有时会根据分类的内容,想让不同的分类以不同的样式展示。通常的方法是在当前主题根目录中多建几个不同布局样式的分类模板,比如category-1.php、category-2.php、category-3.php…..,后面的数字是对应该的分类ID号,或者使用is_category()函数添加判断,操作有些繁琐。有个更简单的方法,安装 Custom Category Templates 插件。

启用插件后,在编辑分类时会添加一个选择模板的选项。

制作几个不同布局风格的页面模板,模板头部必须有类似的标识:

<?php
/*
Template Name: 模板A
*/

然后在编辑或者添加分类时,为不同的分类选择专用的模板即可。

效果如图

下面是从 Custom Category Templates 插件中提取出来的代码,可以直接添加到当前主题函数模板functions.php中即可。

代码版:

// 分类选择模板
class Select_Category_Template{
    public function __construct() {
        add_filter( 'category_template', array($this,'get_custom_category_template' ));
        add_action ( 'edit_category_form_fields', array($this,'category_template_meta_box'));
        add_action( 'category_add_form_fields', array( &$this, 'category_template_meta_box') );
        add_action( 'created_category', array( &$this, 'save_category_template' ));
        add_action ( 'edited_category', array($this,'save_category_template'));
        do_action('Custom_Category_Template_constructor',$this);
    }
    // 添加表单到分类编辑页面
    public function category_template_meta_box( $tag ) {
        $t_id = $tag->term_id;
        $cat_meta = get_option( "category_templates");
        $template = isset($cat_meta[$t_id]) ? $cat_meta[$t_id] : false;
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Template'); ?></label></th>
            <td>
                <select name="cat_template" id="cat_template">
                    <option value='default'><?php _e('Default Template'); ?></option>
                    <?php page_template_dropdown($template); ?>
                </select>
                <br />
                <span class="description"><?php _e('为此分类选择一个模板'); ?></span>
            </td>
        </tr>
        <?php
        do_action('Custom_Category_Template_ADD_FIELDS',$tag);
    }
    // 保存表单
    public function save_category_template( $term_id ) {
        if ( isset( $_POST['cat_template'] )) {
            $cat_meta = get_option( "category_templates");
            $cat_meta[$term_id] = $_POST['cat_template'];
            update_option( "category_templates", $cat_meta );
            do_action('Custom_Category_Template_SAVE_FIELDS',$term_id);
        }
    }
    // 处理选择的分类模板
    function get_custom_category_template( $category_template ) {
        $cat_ID = absint( get_query_var('cat') );
        $cat_meta = get_option('category_templates');
        if (isset($cat_meta[$cat_ID]) && $cat_meta[$cat_ID] != 'default' ){
            $temp = locate_template($cat_meta[$cat_ID]);
            if (!empty($temp))
                return apply_filters("Custom_Category_Template_found",$temp);
        }
        return $category_template;
    }
}
$cat_template = new Select_Category_Template();

以上就是如何为WordPress分类添加选择不同模板选项的详细内容

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

曾凤祥

曾凤祥

技术负责人
小兽WordPress凭借15年的WordPress企业网站开发经验,坚持以“为企业而生的WordPress服务”为宗旨,累计为10万多家客户提供高品质WordPress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽WordPress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部