WordPress分类怎么用不同的模板?Custom Category Templates试试

WordPress分类怎么用不同的模板?Custom Category Templates试试

酱茄 jiangqie.com

WordPress插件Custom Category Templates 是一款简单轻量级插件,允许你为每个类别选择特定模板,就像页面模板一样。

安装方法:后台-插件-搜索: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;

?>


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();

原文来自:https://www.jiangqie.com/jc/6214.html


分享到:


相關文章: