wordpress如何新建、移除文章模板、页面模板

一、新增模板

1、文章模板

别人在插件里写的使用代码添加文章模板:

//$templates = array();
add_filter( 'theme_page_templates', array('add_new_template' ) );
add_filter( 'theme_post_templates', array('add_new_template' ) );
function add_new_template( $posts_templates ) {
$templates = array(
   'essential-blocks-fullwidth-template.php' => __( 'Essential Blocks Fullwidth Template', 'essential_blocks' ),
   'essential-blocks-blank-template.php' => __( 'Essential Blocks Blank Template', 'essential_blocks' ),
);
   $posts_templates = array_merge( $posts_templates, $templates );
   return $posts_templates;
}

Essential Blocks Blank Template的代码:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Name: Essential Blocks Blank Template
 * Description: Blank template for Essential Blocks template
 *
 * @link       https://essential-blocks.com
 * @since      3.3.3
 *
 * @package    Essential Blocks
 */

?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <?php if ( ! current_theme_supports( 'title-tag' ) ) : ?>
        <title><?php echo wp_get_document_title(); ?></title>
    <?php endif; ?>
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

<main class="eb-blank-container">
    <div class="eb-blank-content-wrapper">
        <?php
        /**
         * Hooks anything before content
         *
         * @since 3.3.3
         */
        do_action( 'eb_before_blank_content' );
        ?>

        <?php if ( have_posts() ) : ?>
            <?php
            while ( have_posts() ) :
                the_post();
                ?>
                <?php the_content(); ?>
            <?php endwhile; ?>
        <?php endif; ?>

        <?php
        /**
         * Hooks anything after content
         *
         * @since 3.3.3
         */
        do_action( 'eb_after_blank_content' );
        ?>
    </div>
</main>
<?php wp_footer(); ?>
</body>
</html>

这个插件里面完整的代码:

<?php

if(!class_exists('Essential_Blocks_Page_Template')){
    class Essential_Blocks_Page_Template {

        /**
         * A reference to an instance of this class.
         */
        private static $instance;

        /**
         * The array of templates that this plugin tracks.
         */
        protected $templates;

        /**
         * Returns an instance of this class.
         */
        public static function get_instance() {

            if ( null == self::$instance ) {
                self::$instance = new Essential_Blocks_Page_Template();
            }

            return self::$instance;

        }

        /**
         * Initializes the plugin by setting filters and administration functions.
         */
        private function __construct() {

            $this->templates = array();

            add_filter( 'theme_page_templates', array( $this, 'add_new_template' ) );
            add_filter( 'theme_post_templates', array( $this, 'add_new_template' ) );

            // Add a filter to the save post to inject out template into the page cache.
            add_filter( 'wp_insert_post_data', array( $this, 'register_templates' ) );

            // Add a filter to the template include to determine if the page has our.
            // template assigned and return it's path.
            add_filter( 'template_include', array( $this, 'view_template' ) );

            // Add your templates to this array.
            $this->templates = array(
                'essential-blocks-fullwidth-template.php' => __( 'Essential Blocks Fullwidth Template', 'essential_blocks' ),
                'essential-blocks-blank-template.php' => __( 'Essential Blocks Blank Template', 'essential_blocks' ),
            );

        }

        /**
         * Adds our template to the page dropdown for v4.7+
         */
        public function add_new_template( $posts_templates ) {
            $posts_templates = array_merge( $posts_templates, $this->templates );
            return $posts_templates;
        }

        /**
         * Adds our template to the pages cache in order to trick WordPress
         * into thinking the template file exists where it doens't really exist.
         */
        public function register_templates( $atts ) {

            // Create the key used for the themes cache
            $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );

            // Retrieve the cache list.
            // If it doesn't exist, or it's empty prepare an array.
            $page_templates = wp_get_theme()->get_page_templates();
            if ( empty( $page_templates ) ) {
                $page_templates = array();
            }

            // New cache, therefore remove the old one.
            wp_cache_delete( $cache_key, 'themes' );

            // Now add our template to the list of templates by merging our templates.
            // with the existing templates array from the cache.
            $page_templates = array_merge( $page_templates, $this->templates );

            // Add the modified cache to allow WordPress to pick it up for listing.
            // available templates.
            wp_cache_add( $cache_key, $page_templates, 'themes', 1800 );


            $cache_key_post = 'post_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );

            $post_templates = wp_get_theme()->get_post_templates();

            if ( empty( $post_templates ) ) {
                $post_templates = array();
            }

            if(!isset($post_templates['post'])){
                $post_templates['post'] = array();
            }

            // New cache, therefore remove the old one.
            wp_cache_delete( $cache_key_post, 'themes' );

            // Now add our template to the list of templates by merging our templates.
            // with the existing templates array from the cache.
            $post_templates['post'] = array_merge( $post_templates['post'], $this->templates );

            // Add the modified cache to allow WordPress to pick it up for listing.
            // available templates.
            wp_cache_add( $cache_key_post, $post_templates, 'themes', 1800 );

            return $atts;

        }

        /**
         * Checks if the template is assigned to the page
         */
        public function view_template( $template ) {

            // Get global post.
            global $post;

            // Return template if post is empty.
            if ( ! $post ) {
                return $template;
            }

            // Return default template if we don't have a custom one defined.
            if ( ! isset(
                $this->templates[ get_post_meta(
                    $post->ID,
                    '_wp_page_template',
                    true
                ) ]
            ) ) {
                return $template;
            }

            $file = ESSENTIAL_BLOCKS_DIR_PATH . 'templates/' . get_post_meta(
                $post->ID,
                '_wp_page_template',
                true
            );

            // Just to be safe, we check if the file exist first.
            if ( file_exists( $file ) ) {
                return $file;
            } else {
                echo esc_html( $file );
            }

            // Return template.
            return $template;

        }
    }
}

当然,我们也能通过修改主题文件的方式,添加文章模板,你可以看看:

2、页面

新建的话也可以看看上面的代码,上面的代码是文章模板和页面模板都新建了,当然,我还有两个插件分享我直接放出来吧,看完这两个插件也能快速了解新建页面模板,文件登录可见:

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享