
您现在的位置是:首页 > WordPress教程WordPress教程
WordPress自定义主题开发教程
WP集市
2025-08-27
【WordPress教程】
1424人已围观
- 想搞个自己的WordPress主题?别慌,从建文件夹开始。在你电脑的wp-content/themes里头,新整个目录,比如叫my_theme。里面必须塞俩文件:index.php和style.css。style.css得写清楚主题信息,像这样:
/*
Theme Name: 我的自定义主题
Author: 你的名字
Version: 1.0
*/
- 然后折腾header.php和footer.php。把博客头部和脚部分开写,用
get_header()
和get_footer()
调用。比如在header.php里别忘了加wp_head()
,不然插件会闹脾气:
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<?php wp_head(); ?>
</head>
- 主循环是核心!在index.php里用这个套路抓内容。WordPress靠loop吃饭,就像下面这样绕圈输出文章:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; endif; ?>
- 想自定义页面?复制个page.php就行。用模板层级规则,比如single.php管单篇文章,archive.php管分类页。要搞特殊页面,直接创建模板文件,开头加注释:
<?php /* Template Name: 全宽页面 */ ?>
- 最后用functions.php加功能。这里能挂钩子、注册菜单,比如让主题支持自定义logo:
add_theme_support( 'custom-logo' );
add_theme_support( 'post-thumbnails' );
记得调样式用wp_enqueue_style,别直接硬写link标签。做完激活主题,慢慢调试——主题开发就是不断拆了修、修了拆的循环。
Tags:
文章版权声明:除非注明,否则均为WP集市原创文章,转载或复制请以超链接形式并注明出处。

热门文章
