欢迎来到路由器窝!

列表自定义模板

浏览次数:

作者: 小编

信息来源:

更新日期: 2024-03-04 00:12

文章简介

首先需要先定义下模板名字在模板最上方添加<?php /* Template Name: application_area_list */?>添加函数//指定文章模板function load_single_template($template

  • 正文开始
  • 相关文章

首先需要先定义下模板名字在模板最上方添加

<?php /* Template Name: application_area_list */?>
添加函数


//指定文章模板
function load_single_template($template) {
    $new_template = '';
    // single post template    
    if( is_single() ) {      
        global $post;
        // 'wordpress' is category slugs   
        // 指定分类的别名,设置不同的文章模板   2020-8-23
        if( has_term('fangan', 'category', $post) ) {
        // use template file single-wordpress.php        
            $new_template = locate_template(array('single-wordpress.php' ));
        }

        if( has_term('case', 'category', $post) ) {
        // use template file single-wordpress.php        
            $new_template = locate_template(array('single-case.php' ));
        }
    }    
    return ('' != $new_template) ? $new_template : $template;  
}
add_action('template_include', 'load_single_template');


// 分类选择模板
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» 列表自定义模板

标签:
  • wordpress如何设置禁止/禁用插件更新-WordPress主题
    693阅读
    add_filter(‘pre_site_transient_update_core’, create_function(‘$a’, “return null;”)); // 关闭核心提示 add_filter(‘pre_site_transient_update_plugins’, create_functio...
  • 后台菜单a标签添加样式
    703阅读
    function xintheme_menu_link_atts( $atts, $item, $args ) { $atts['class'] = 'menu-link'; $atts['data-link-alt'] = $item->title; ret...
  • wordpress自定义菜单
    841阅读
    注册菜单if(function_exists('register_nav_menus')){register_nav_menus(array('header-menu' => __( '导航自定义菜单' ),));}调用菜单<?php wp_nav_menu(...
  • 添加基本编辑字段公共字段
    728阅读
    //添加自定义字段function my_profile( $contactmethods ) {$contactmethods['tel'] = 'tel'; //增加$contactmethods['fax'] = 'fax';$contactmeth...
  • 添加栏目编辑器
    552阅读
    //添加栏目编辑器add_action("category_edit_form_fields", 'add_form_fields_example', 10, 2);function add_form_fields_example($term, $taxonomy){echo '<tr val...
  • 分类栏目图片插件
    736阅读
    调用方法<?php get_cat_icon(); ?>
  • php if  ifelse 循环
    576阅读
    <?php if (condition1): ?> <!-- code to be executed if condition1 is true --> <?php elseif (condition2): ?> <!-...
  • WordPress True / False功能参数
    929阅读
    https://www.advancedcustomfields.com/resources/true-false/ True / False 字段返回布尔值true或false。请注意,保存到数据库中的实际值是 1 或 0 的整数。<?phpif( get_field('color') ) { ...
  • WordPress 无插件 纯代码实现分页导航
    198阅读
    第一步:将以下代码放到自己使用的wordpress主题的模板函数文件functions.php中。 function kriesi_pagination($query_string){global $posts_per_page, $paged;$my_query = new WP_Query($query_strin...
  • wordpress常用函数
    250阅读
    ?phprequire get_template_directory() . /inc/helper.php;add_filter(acf/settings/path, my_acf_settings_path); function my_acf_settings_path( $path ) { $path = get...
上一页:栏目排序
下一页:自定义面包屑
最近更新作品
acf判断
更新时间:2024-04-17

891人已经看过了!

指定栏目ID另类调用配合acf
更新时间:2024-04-16

857人已经看过了!

word press 自定义文章类型制作留言功能
更新时间:2024-03-10

985人已经看过了!

php if  ifelse 循环
更新时间:2024-03-09

576人已经看过了!

栏目模板选择插件栏目模板
更新时间:2024-03-08

738人已经看过了!

contact-form-7历史版本WP SMTP配合使用
更新时间:2024-03-08

809人已经看过了!

WordPress True / False功能参数
更新时间:2024-03-08

929人已经看过了!

wordpress调用指定分类置顶文章
更新时间:2024-03-07

519人已经看过了!

WordPress父分类调用子分类名称和文章列表
更新时间:2024-03-07

519人已经看过了!