怎样给wordpress的分类设置不同的模板

将下面的代码添加到主题的functions.php就可以给分类添加分类模板

复制代码
//指定文章模板
function load_single_template($template) {
    $new_template = '';
    // single post template    
    if( is_single() ) {      
        global $post;
        // 'wordpress' is category slugs   
        // 指定分类的别名,设置不同的文章模板   2020-8-23
        if( has_term('fangan', 'category', $post) ) {
        // use template file single-wordpress.php        
            $new_template = locate_template(array('single-wordpress.php' ));
        }

        if( has_term('case', 'category', $post) ) {
        // use template file single-wordpress.php        
            $new_template = locate_template(array('single-case.php' ));
        }
    }    
    return ('' != $new_template) ? $new_template : $template;  
}
add_action('template_include', 'load_single_template');


// 分类选择模板
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();
复制代码

 

posted @   星锋  阅读(759)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示