首页 视频课程 WordPress开发文档分类标签与页面 插件页面

插件页面

2020-11-01 / 7161阅

本视频需要登录以后才能播放

登录

WordPress可以在主题文件的头部添加注释来实现创建一个通用的页面模板。但是在插件中,此方法无效。所有我们需要使用下面的代码来创建通用页面

<?php
       class PageTemplater {
              private static $instance;
              protected $templates;
              public static function get_instance() {
                     if ( null == self::$instance ) {
                            self::$instance = new PageTemplater();
                     }
                     return self::$instance;
              }
              private function __construct() {
                     $this->templates = array();
                     if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
                            add_filter(
                                   'page_attributes_dropdown_pages_args',
                                   array( $this, 'register_project_templates' )
                            );
                     } else {
                            add_filter(
                                   'theme_page_templates', array( $this, 'add_new_template' )
                            );
                     }
                     add_filter(
                            'wp_insert_post_data',
                            array( $this, 'register_project_templates' )
                     );
                     add_filter(
                            'template_include',
                            array( $this, 'view_project_template')
                     );
                     $this->templates = array(
                            'page.php' => '导航首页',
                     );
              }
              public function add_new_template( $posts_templates ) {
                     $posts_templates = array_merge( $posts_templates, $this->templates );
                     return $posts_templates;
              }
              public function register_project_templates( $atts ) {
                     $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
                     $templates = wp_get_theme()->get_page_templates();
                     if ( empty( $templates ) ) {
                            $templates = array();
                     }
                     wp_cache_delete( $cache_key , 'themes');
                     $templates = array_merge( $templates, $this->templates );
                     wp_cache_add( $cache_key, $templates, 'themes', 1800 );
                     return $atts;
              }
              public function view_project_template( $template ) {
                     global $post;
                     if ( ! $post ) {
                            return $template;
                     }
                     if ( ! isset( $this->templates[get_post_meta(
                                   $post->ID, '_wp_page_template', true
                            )] ) ) {
                            return $template;
                     }
                     $file = plugin_dir_path( __FILE__ ). get_post_meta(
                                   $post->ID, '_wp_page_template', true
                            );
                     if ( file_exists( $file ) ) {
                            return $file;
                     } else {
                            echo $file;
                     }
                     return $template;
              }
       }
       add_action( 'init', array( 'PageTemplater', 'get_instance' ) ); 

这文件中第31代码开始可以自由替换:

$this->templates = array(
       'page.php' => '导航首页',
); 

page.php是你的模板文件名称和路径。后面是显示在后面页面模板中的模板名称。

可以把这个文件单独放出来,然后引入这个文件就可以了。

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录