Home > CMS Tutorial > WordPress > How to Add Choose Different Template Option to WordPress Categories

How to Add Choose Different Template Option to WordPress Categories

藏色散人
Release: 2019-11-13 11:57:05
forward
3252 people have browsed it

The following column WordPress Tutorial will show you how to add different template options to WordPress categories. I hope it will be helpful to friends in need!

How to Add Choose Different Template Option to WordPress Categories

We sometimes want different categories to be displayed in different styles based on the content of the category. The usual method is to create several more category templates with different layout styles in the current theme root directory, such as category-1.php, category-2.php, category-3.php..., the following numbers are for The appropriate category ID number, or use the is_category() function to add a judgment, which is a bit cumbersome. There is an easier way, install the Custom Category Templates plugin.

After enabling the plug-in, an option to select a template will be added when editing a category.

Create several page templates with different layout styles. The header of the template must have a similar logo:

<?php
/*
Template Name: 模板A
*/
Copy after login

Then when editing or adding categories, just select dedicated templates for different categories. .

The effect is as shown:

How to Add Choose Different Template Option to WordPress Categories

The following is the code extracted from the Custom Category Templates plug-in, which can be added directly to the current theme The function template can be found in functions.php.

Code version:

// 分类选择模板
class Select_Category_Template{
	public function __construct() {
		add_filter( &#39;category_template&#39;, array($this,&#39;get_custom_category_template&#39; ));
		add_action ( &#39;edit_category_form_fields&#39;, array($this,&#39;category_template_meta_box&#39;));
		add_action( &#39;category_add_form_fields&#39;, array( &$this, &#39;category_template_meta_box&#39;) );
		add_action( &#39;created_category&#39;, array( &$this, &#39;save_category_template&#39; ));
		add_action ( &#39;edited_category&#39;, array($this,&#39;save_category_template&#39;));
		do_action(&#39;Custom_Category_Template_constructor&#39;,$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(&#39;Category Template&#39;); ?></label></th>
			<td>
				<select name="cat_template" id="cat_template">
					<option value=&#39;default&#39;><?php _e(&#39;Default Template&#39;); ?></option>
					<?php page_template_dropdown($template); ?>
				</select>
				<br />
				<span class="description"><?php _e(&#39;为此分类选择一个模板&#39;); ?></span>
			</td>
		</tr>
		<?php
		do_action(&#39;Custom_Category_Template_ADD_FIELDS&#39;,$tag);
	}
 
	// 保存表单
	public function save_category_template( $term_id ) {
		if ( isset( $_POST[&#39;cat_template&#39;] )) {
			$cat_meta = get_option( "category_templates");
			$cat_meta[$term_id] = $_POST[&#39;cat_template&#39;];
			update_option( "category_templates", $cat_meta );
			do_action(&#39;Custom_Category_Template_SAVE_FIELDS&#39;,$term_id);
		}
	}
 
	// 处理选择的分类模板
	function get_custom_category_template( $category_template ) {
		$cat_ID = absint( get_query_var(&#39;cat&#39;) );
		$cat_meta = get_option(&#39;category_templates&#39;);
		if (isset($cat_meta[$cat_ID]) && $cat_meta[$cat_ID] != &#39;default&#39; ){
			$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();
Copy after login

The above is the detailed content of How to Add Choose Different Template Option to WordPress Categories. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template