腾讯云服务器优惠活动华纳云香港服务器

怎么让WordPress分类目录像页面那样也有个模板选择?

WordPress主题推荐

我们在新建/编辑页面的时候有一个模板选择选项,不用的页面可以选用不同的模板,而我们在新建/编辑分类目录的时候却没有这个模板选项,想要实现不用分类目录显示不用的布局只能通过最笨的方法,也就是通过这样 category-1.php、category-2.php、category-3.php…..,后面的数字是对应该的分类 ID 号,或者使用 is_category()函数添加判断使用。今天跟大家介绍两个比较简单的办法,可以直接让分类目录像页面那样也有一个模板选项。

前提条件

下文中的内容不管是插件实现还是纯代码实现,前提都是需要建立有分类目录的相应模板。建立模板也很简单,可以找主题的 category.php(PS:如果没有该文件就找 archive.php 文件)复制成另一个文件,可以命名为 template-cat1.php、template-cat2.php、template-cat3.php、……,这些文件的开头都必须添加以下代码:

<?php
/*
Template Name: 分类目录 3 模板
*/
?>

具体如下图所示:

怎么让WordPress分类目录像页面那样也有个模板选择?-第1张-boke112百科(boke112.com)

其中中文部分的命名可以随便修改,Template Name:不能修改。

插件方法实现分类目录添加模板选项

在 WordPress 后台直接搜索并安装并启用 Custom Category Template 插件(PS:搜索结果不是第一个,而是要往下拖动寻找)。或直接到 Custom Category Template 插件官方介绍页下载(点此直达)后上传到 wp-content\plugins\文件夹内后到后台启用。

怎么让WordPress分类目录像页面那样也有个模板选择?-第2张-boke112百科(boke112.com)

成功启用之后无需设置,直接新建分类目录或编辑目录的时候都可以看到多了一个“分类目录模板”的选项,内容就是我们前面建立的分类目录模板。具体如下图所示:

怎么让WordPress分类目录像页面那样也有个模板选择?-第3张-boke112百科(boke112.com)

纯代码方法实现分类目录添加模板选项

有些站长不太喜欢 WordPress 插件,所以这里是从 Custom Category Template 插件中提取出来的代码,可以直接添加到当前主题函数模板 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_SAVE_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();

添加以上代码之后同样不用什么设置,同样直接新建/编辑分类目录就能看到多了一个“分类目录模板”的选项。具体如下图所示:

怎么让WordPress分类目录像页面那样也有个模板选择?-第4张-boke112百科(boke112.com)

特别声明:以上答案内容整理自知更鸟大神的『为 WordPress 分类添加选择不同模板选项』。

本文地址:https://boke112.com/post/6695.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu