登录  /  注册
首页 > CMS教程 > WordPress > 正文

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

藏色散人
发布: 2019-11-13 11:57:05
转载
3011人浏览过

下面由wordpress教程栏目给大家为wordpress分类添加选择不同模板选项的方法,希望对需要的朋友有所帮助!

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

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

启用插件后,在编辑分类时会添加一个选择模板的选项。

制作几个不同布局风格的页面模板,模板头部必须有类似的标识:

<?php
/*
Template Name: 模板A
*/
登录后复制

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

效果如图:

69e58ca548c9d3069a7129f8875b66a.png

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

代码版:

// 分类选择模板
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();
登录后复制

以上就是如何为WordPress分类添加选择不同模板选项的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:zmingcx网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号