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

然后在编辑或者添加分类时,为不同的分类选择专用的模板即可,效果如图:
WordPress分类添加不同模板选项怎么弄?-指点聚

方法二:

下面是从 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分类添加不同模板选项怎么弄?上述就是简单步骤介绍,如有问题可以在下方进行评论交流。

给TA打赏
共{{data.count}}人
人已打赏
WP实用教程

宝塔面板默认WordPress伪静态规则的调整-指点聚

2018-12-24 11:42:12

WP实用教程

为WordPress资讯/博客主题添加文章字数统计和阅读时间预计

2019-1-10 22:23:05


版权免责声明

本资源仅用于个人学习测试使用,禁止用于任何商业环境,请于下载后24小时内 删除本资源!

本文所有相关技术/资源内容统一放在下载包里

  1. 1、本网站名称:指点聚源码
  2. 2、本站永久网址:https://www.zhidianju.com
  3. 3、站内所有资源均可通过日常签到等任务进行免费兑换,记得每日来本站签到哦~!(由于模板不是很完善签到 需刷新一下!)。
  4. 4、如果您觉得本站有帮助到您,也想给本站一些帮助。您可以再下方打赏本站。
  5. 5、如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
  6. 6、本站资源来源于互联网收集/个人购买,如有侵犯到您的权益,请查看【版权声明】
  7. 7、本站提供的所有资源均来自互联网搜索,站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁 止用于任何商业环境,任何人不得擅作它用,否则后果自负!请在下载24小时内删除!为尊重作者版权,请购买原版作品,支 持你喜欢的作者,谢谢!
  8. 8、本站资源大多存储在云盘,如发现链接失效 【链接失效】 我们会第一时间更新。本站提供的源码、模板、软件工具等其他资源, 都不包含技术服务 请大家谅解!
  9. 9、如遇到加密压缩包,默认解压密码为"www.zhidianju.com",如遇到无法解压的请联系管理员!
  10. 10、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索