首页 问答 正文

如何创建一个自定义的wordpress主题?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 123 次

创建一个自定义的 WordPress 主题需要遵循以下步骤:

  1. 创建主题文件夹

在 WordPress 的主题目录(/wp-content/themes/)下创建一个新的文件夹作为主题文件夹,例如:

/wp-content/themes/my-theme/ 
  1. 创建样式表

在主题文件夹中创建一个名为 style.css 的样式表文件,并添加以下头部信息:

/*
Theme Name: My Theme
Theme URI: http://example.com/
Author: Your Name
Author URI: http://example.com/
Description: A brief description of your theme.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-theme
*/ 

这些信息将用于描述和标识您的主题。您还可以根据需要添加其他样式和 JavaScript 文件。

  1. 创建主题模板文件

创建主题的核心文件是主题模板文件。WordPress 主题通常包含以下模板文件:

  • index.php:主页模板
  • header.php:头部模板
  • footer.php:底部模板
  • single.php:单页或文章模板
  • page.php:页面模板
  • archive.php:档案模板
  • search.php:搜索结果模板

您可以根据需要创建其他模板文件。

  1. 添加WordPress功功能

您的主题可能需要某些特定的 WordPress 功能。要添加这些功能,请在 functions.php 文件中添加代码。例如,要添加支持特色图片的功能,请添加以下代码:

add_theme_support( 'post-thumbnails' ); 
  1. 添加页面模板

如果您想要自定义某些页面的模板,请在主题文件夹中创建一个名为 page-templatename.php 的文件,其中 templatename 是您选择的模板名称。然后,您可以在 WordPress 后台的页面编辑器中选择该模板。

  1. 添加自定义菜单

您可以使用 WordPress 的自定义菜单功能添加菜单。要添加一个自定义菜单,请在 functions.php 文件中添加以下代码:

register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'my-theme' ),
) ); 

然后,在主题模板中添加以下代码以显示菜单:

wp_nav_menu( array(
    'theme_location' => 'primary',
    'menu_class'     => 'my-menu',
) ); 

这将显示名为 “Primary Menu” 的菜单。

示例代码:

/*
Theme Name: My Theme
Theme URI: http://example.com/
Author: Your Name
Author URI: http://example.com/
Description: A brief description of your theme.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-theme
*/

/* Add your custom styles here */ 
<?php
/**
 * The template for displaying the header.
 *
 * @package WordPress
 * @subpackage My_Theme
 * @since My Theme 1.0
 */
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><?php wp_title( '|', true, 'right' ); ?></title>
    <?php wp_head(); ?>
</head>

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

<header>
    <nav>
        <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
    </nav>
</header> 
<?php
/**
 * The template for displaying the footer.
 *
 * @package WordPress
 * @subpackage My_Theme
 * @since My Theme 1.0
 */
?>
<footer>
    <?php wp_footer(); ?>
</footer>
</body>
</html> 
<?php
/**
 * The main template file.
 *
 * @package WordPress
 * @subpackage My_Theme
 * @since My Theme 1.0
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', get_post_format() ); ?>

            <?php endwhile; ?>

            <?php twentyfifteen_paging_nav(); ?>

        <?php else : ?>

            <?php get_template_part( 'content', 'none' ); ?>

        <?php endif; ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_footer(); ?> 
大家谈论
    我的见解