您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
为WordPress分类添加选择不同模板选项的方法
乐萱2023-06-24WordPress教程已有人查阅
导读我们有时会根据分类的内容,想让不同的分类以不同的样式展示。通常的方法是在当前主题根目录中多建几个不同布局样式的分类模板,比如category-1.php、category-2.php、category-3.php.....

我们有时会根据分类的内容,想让不同的分类以不同的样式展示。通常的方法是在当前主题根目录中多建几个不同布局样式的分类模板,比如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_S E_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();
本文标签:
很赞哦! ()
图文教程
LNMP架构搭建wordpress网站的方法
回到顶部首先部署mysql数据库二进制包方式安装mysql数据库软件下载解压mysql软件创建mysql管理用户useradd -s /sbin/nologin -M mysql给mysql放到一个目录中,设置软连接
WordPress添加广告位的方法
1.在Wordpress侧边栏添加广告:进入博客后台小工具widgets,将Text工具拖入侧边栏,展开Text 直接将广告代码粘贴进去保存即可。2.在Wordpress文章內容顶部加广告:打开主题目录下的
怎么打开Wordpress调试模式进行测试
下面由代码号给大家介绍如何Wordpress打开调试模式,希望对需要的朋友有所帮助!Wordpress打开调试模式:
wordpress官网改成中文简体的方法
其实我们在从WordPress官网网站下载的程序安装的时候,向导会自动切换让我们选择安装语言,但是有些时候确实还不会提示出来选择语言
相关源码
-
(自适应响应式)HTML5甲醛环境检测网站模板带在线留言和资料下载本模板为甲醛检测与环保科技企业开发,采用PbootCMS内核构建。首页集成空气质量数据可视化模块,服务流程采用时间轴展示设计,检测报告板块支持PDF在线预览功能查看源码 -
自适应LED照明外贸灯具灯泡灯具英文网站模板该外贸灯具网站模板专为LED照明、灯具出口企业定制,采用PbootCMS内核开发,提供高效建站方案。通过响应式设计和SEO优化能力,帮助企业低成本构建专业外贸展示平台。查看源码 -
(PC+WAP)安保服务保安保镖模板免费下载本模板基于PbootCMS内核开发,为安保服务企业量身打造。设计风格严谨专业,突出安保行业的安全、可靠特性,展示企业服务项目与实力。采用响应式设计,PC与移动端数据同步,管理便捷。模板布局合理查看源码 -
PbootCMS(自适应手机端)智能电话AI机器人网站模板产品概述基于PbootCMS内核开发的智能电话AI机器人网站模板,为语音交互服务场景设计,采用HTML5+CSS3响应式布局技术,实现PC端与移动端数据实时同步展示。模板适用于智能电话查看源码 -
(自适应手机端)锁锁芯锁具网站pbootcms模板 智能防盗锁网站源码下载本模板基于PbootCMS系统开发,为智能锁具、防盗锁芯及相关安防产品企业设计。采用响应式布局技术,确保在手机、平板和电脑等不同设备上都能获得良好的浏览体验,数据实时同步更新。查看源码 -
自适应APP应用程序介绍推广落地页pbootcms网站源码下载移动应用开发商设计的营销型落地页模板,基于PbootCMS内核深度开发采用前沿响应式架构,无缝适配手机端操作习惯与PC端展示需求。查看源码
| 分享笔记 (共有 篇笔记) |

