欢迎光临
我们一直在努力

WordPress怎么给分类目录选择其他模板纯代码无需插件

在很多时候,我们想把个别的分类目录的列表样式修改成不一样的,那么如何想改成我们想要的效果呢?今天新起点博客就来告诉如何实现分类目录选择其他模板,纯代码无需插件;

复制以下代码到functions.php文件家中(后台外观——主题编辑器——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();

 

添加完以上代码保存完,在编辑我们的分类目录的时候就会出现选择项;

如果没有自己想要用的模板页面,我们可以自定义添加,首先创建一个.php文件,比如说共享资源网创建的一个template-tuwen.php文件,但是必须记住一点,一定要将一下代码复制到php文件的上面。然后再把自己喜欢的页面粘贴到下面,或者是自己写的代码粘贴过去。

<?php
/*
Template Name: 图文列表
*/
?>

 

赞(0) 打赏
未经允许不得转载:新起点博客 » WordPress怎么给分类目录选择其他模板纯代码无需插件


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏