上面是搬码源码网 abnma.com 经由过程收集网络收拾整顿的代码片断。搬码源码网小编此刻分享给各人,也给各人做个参考。
咱们有时会按照分类的内容,想让差别的分类以差别的样式铺示。凡是的要领是在以后主题根目次中多建几个差别结构样式的分类模板,好比 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','category_template_meta_box')); add_action( 'category_add_form_fields',array( &$this,'category_template_meta_box') ); add_action( 'created_category','save_category_template' )); add_action ( 'edited_category','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();
以上是搬码源码网(abnma.com)为你网络收拾整顿的全数代码内容,但愿文章可以或许帮你解决所碰到的步伐开提问题。假如感觉搬码源码网网站内容还不错,接待将搬码源码网网站保举给步伐员挚友。
总结
以上是搬码源码网为你网络收拾整顿的为WordPress分类添加抉择差别模板选项全数内容,但愿文章可以或许帮你解决为WordPress分类添加抉择差别模板选项所碰到的步伐开提问题。
假如感觉搬码源码网网站内容还不错,接待将搬码源码网网站保举给步伐员挚友。
请先
!