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

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

作者 : 资源客 发布时间: 2019-11-23

我们有时会根据分类的内容,想让不同的分类以不同的样式展示。通常的方法是在当前主题根目录中多建几个不同布局样式的分类模板,比如 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();

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » 为WordPress分类添加选择不同模板选项