search
HomeCMS TutorialWordPressHow to Add Choose Different Template Option to WordPress Categories

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
*/

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

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!

Statement
This article is reproduced at:zmingcx. If there is any infringement, please contact admin@php.cn delete
Is WordPress good for creating a portfolio website?Is WordPress good for creating a portfolio website?Apr 26, 2025 am 12:05 AM

Yes,WordPressisexcellentforcreatingaportfoliowebsite.1)Itoffersnumerousportfolio-specificthemeslike'Astra'foreasycustomization.2)Pluginssuchas'Elementor'enableintuitivedesign,thoughtoomanycanslowthesite.3)SEOisenhancedwithtoolslike'YoastSEO',boosting

What are the advantages of using WordPress over coding a website from scratch?What are the advantages of using WordPress over coding a website from scratch?Apr 25, 2025 am 12:16 AM

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

What makes WordPress a Content Management System?What makes WordPress a Content Management System?Apr 24, 2025 pm 05:25 PM

WordPressisaCMSduetoitseaseofuse,customization,usermanagement,SEO,andcommunitysupport.1)Itsimplifiescontentmanagementwithanintuitiveinterface.2)Offersextensivecustomizationthroughthemesandplugins.3)Providesrobustuserrolesandpermissions.4)EnhancesSEOa

How to add a comment box to WordPressHow to add a comment box to WordPressApr 20, 2025 pm 12:15 PM

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy sub-sites from wordpressHow to copy sub-sites from wordpressApr 20, 2025 pm 12:12 PM

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

How to write a header of a wordpressHow to write a header of a wordpressApr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to display wordpress commentsHow to display wordpress commentsApr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

How to upload source code for wordpressHow to upload source code for wordpressApr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft