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、页面

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

再放一个子比主题的新建页面的代码,而且还可以根据这个函数获取页面的链接,代码如下:

/**
 * @description: 根据页面模板获取页面链接
 * @param {*} $template
 * @return {*}
 */
function zib_get_template_page_url($template, $args = array())
{
    $cache = wp_cache_get($template, 'page_url', true);
    if ($cache) {
        return $cache;
    }

    $templates = array(
        'pages/newposts.php'  => array('发布文章', 'newposts'),
        'pages/user-sign.php' => array('登录/注册/找回密码', 'user-sign'),
        'pages/download.php'  => array('资源下载', 'download'),
    );
    $templates  = array_merge($templates, $args);
    $pages_args = array(
        'meta_key'   => '_wp_page_template',
        'meta_value' => $template,
    );
    $pages   = get_pages($pages_args);
    $page_id = 0;
    if (!empty($pages[0]->ID)) {
        $page_id = $pages[0]->ID;
    } elseif (!empty($templates[$template][0])) {
        $one_page = array(
            'post_title'  => $templates[$template][0],
            'post_name'   => $templates[$template][1],
            'post_status' => 'publish',
            'post_type'   => 'page',
            'post_author' => 1,
        );

        $page_id = wp_insert_post($one_page);
        update_post_meta($page_id, '_wp_page_template', $template);
    }
    if ($page_id) {
        $url = get_permalink($page_id);
        wp_cache_set($template, $url, 'page_url');
        return $url;
    } else {
        return false;
    }
}
//获取链接
$href = zib_get_template_page_url('pages/newposts.php');
$url = zib_get_template_page_url('pages/user-sign.php');
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容