wordpress主题开发001——新建主题

本文将引导你创建一个专为日常笔记设计的WordPress主题,包括必备文件结构、关键代码片段和自定义功能。从style.css到functions.php,一步步教你如何实现一个响应式、定制化的博客模板。

参考资料

官方文档:

https://developer.wordpress.org/themes/

油管视频:

https://www.youtube.com/watch?v=oTRZYnYQlmo&list=PLriKzYyLb28nUFbe0Y9d-19uVkOnhYxFE

新建主题所需文件

wp-content\themes\cznoteblog(创建主题文件夹,目录名称选择一个全网唯一的)

style.css(必须),其中包含了主题的meta信息

/*
    Theme Name: CZ Note Blog
    Theme URI: http://www.wudics.com/cznoteblog
    Author: wudics
    Author URI: http://www.wudics.com
    Description: This is a blog theme for daily note.
    Version: 0.1 alpha
    License: GNU General Public License v2 or later
    License URI: http://www.gnu.org/licenses/gpl-2.0.html
    Tags: black, white, responsive, one-colum, two-colums, featured-image, custom-menu, custom-header, post-formats
*/

index.php(必须),如下是示例代码

<?php get_header(); ?>

    <?php
    if (have_posts()):
        while (have_posts()): the_post();
    ?>
            <h3><?php the_title(); ?></h3>
            <small>Posted on: <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>, in <?php the_category(); ?></small>

            <p><?php the_content(); ?></p>
            <hr />
    <?php
        endwhile;
    endif;
    ?>

<?php get_footer(); ?>

header.php(被index.php加载)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>CZ NOTE BLOG</title>
        <?php wp_head(); ?>
    </head>

    <?php
        // 检测后台配置的首页用is_front_page(),检测后台配置的文章页用is_home()
        if (is_front_page()):
            $cznoteblog_classes = array('cznoteblog-class', 'hello-class');
        else:
            $cznoteblog_classes = array('no-cznoteblog-class');
        endif;
    ?>

    <body <?php body_class($cznoteblog_classes); ?>>

        <?php wp_nav_menu(array('theme_location' => 'primary')); ?>

footer.php(被index.php加载)

        <footer>
            <p>my footer</p>
            <?php wp_nav_menu(array('theme_location' => 'secondary')); ?>
        </footer>
        
        <?php wp_footer(); ?>
    </body>
</html>

screenshot.png(1200*900)主题封面

functions.php(钩子函数),如下是自定义加载css、js,以及自定义menus

<?php

function cznoteblog_script_enqueue() {

    wp_enqueue_style('customstyle', get_template_directory_uri() . '/css/cznoteblog.css', array(), '1.0.0', 'all');
    wp_enqueue_script('customjs', get_template_directory_uri() . '/js/cznoteblog.js', array(), '1.0.0', true);

}
add_action('wp_enqueue_scripts', 'cznoteblog_script_enqueue');

function cznoteblog_theme_setup() {

    add_theme_support('menus');

    register_nav_menu('primary', 'Primary Header Navigation');
    register_nav_menu('secondary', 'Footer Navigation');
}
add_action('init', 'cznoteblog_theme_setup');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wudics

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值