WordPress教程

为WordPress分类添加选择不同模板选项

阿里云

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

方法一:

有个更简单的方法,安装 Custom Category Templates 插件。启用插件后,在编辑分类时会添加一个选择模板的选项。制作几个不同布局风格的页面模板,模板头部必须有类似的标识:

也想出现在这里?联系我们
创客主机
  1.     <?php
  2.     /*
  3.     Template Name: 模板A
  4.     */

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

方法二:

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

  1.     // 分类选择模板
  2.     class Select_Category_Template{
  3.     	public function __construct() {
  4.     		add_filter( 'category_template', array($this,'get_custom_category_template' ));
  5.     		add_action ( 'edit_category_form_fields', array($this,'category_template_meta_box'));
  6.     		add_action( 'category_add_form_fields', array( &$this, 'category_template_meta_box') );
  7.     		add_action( 'created_category', array( &$this, 'save_category_template' ));
  8.     		add_action ( 'edited_category', array($this,'save_category_template'));
  9.     		do_action('Custom_Category_Template_constructor',$this);
  10.     	}
  11.  
  12.     	// 添加表单到分类编辑页面
  13.     	public function category_template_meta_box( $tag ) {
  14.     		$t_id = $tag->term_id;
  15.     		$cat_meta = get_option( "category_templates");
  16.     		$template = isset($cat_meta[$t_id]) ? $cat_meta[$t_id] : false;
  17.     		?>
  18.     		<tr class="form-field">
  19.     			<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Template'); ?></label></th>
  20.     			<td>
  21.     				<select name="cat_template" id="cat_template">
  22.     					<option value='default'><?php _e('Default Template'); ?></option>
  23.     					<?php page_template_dropdown($template); ?>
  24.     				</select>
  25.     				<br />
  26.     				<span class="description"><?php _e('为此分类选择一个模板'); ?></span>
  27.     			</td>
  28.     		</tr>
  29.     		<?php
  30.     		do_action('Custom_Category_Template_ADD_FIELDS',$tag);
  31.     	}
  32.  
  33.     	// 保存表单
  34.     	public function save_category_template( $term_id ) {
  35.     		if ( isset( $_POST['cat_template'] )) {
  36.     			$cat_meta = get_option( "category_templates");
  37.     			$cat_meta[$term_id] = $_POST['cat_template'];
  38.     			update_option( "category_templates", $cat_meta );
  39.     			do_action('Custom_Category_Template_SAVE_FIELDS',$term_id);
  40.     		}
  41.     	}
  42.  
  43.     	// 调用所有页面模板
  44.     	function get_custom_category_template( $category_template ) {
  45.     		$cat_ID = absint( get_query_var('cat') );
  46.     		$cat_meta = get_option('category_templates');
  47.     		if (isset($cat_meta[$cat_ID]) && $cat_meta[$cat_ID] != 'default' ){
  48.     			$temp = locate_template($cat_meta[$cat_ID]);
  49.     			if (!empty($temp))
  50.     				return apply_filters("Custom_Category_Template_found",$temp);
  51.     		}
  52.     		return $category_template;
  53.     	}
  54.     }
  55.  
  56.     $cat_template = new Select_Category_Template();

WordPress 分类添加选择不同模板选项

已有 433 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!