在WordPress主题开发中,get_header()
和 get_footer()
是两个非常常用的模板函数,它们分别用于包含页眉(header)和页脚(footer)模板。以下是使用这两个函数的基本步骤:
1. 创建页眉模板(header.php)
在你的WordPress主题目录中,创建一个名为 header.php
的文件。这个文件将包含网站的页眉部分,通常包括网站的标题、导航菜单、logo等。
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<title><?php wp_title( '|', true, 'right' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id="page" class="hfeed site">
<header id="masthead" class="site-header" role="banner">
<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</header>
2. 创建页脚模板(footer.php)
同样,在你的WordPress主题目录中,创建一个名为 footer.php
的文件。这个文件将包含网站的页脚部分,通常包括版权信息、链接到隐私政策页面等。
<footer id="colophon" class="site-footer" role="contentinfo">
<div class="site-info">
<?php do_action( 'twentyfourteen_credits' ); ?>
<a href="<?php echo esc_url( __( 'https://wordpress.org/', 'twentyfourteen' ) ); ?>" title="<?php esc_attr_e( 'Semantic Personal Publishing Platform', 'twentyfourteen' ); ?>"><?php printf( __( 'Proudly powered by %s', 'twentyfourteen' ), 'WordPress' ); ?></a>
</div><!-- .site-info -->
</footer><!-- #colophon -->
<?php wp_footer(); ?>
</body>
</html>
3. 在其他模板文件中使用 get_header() 和 get_footer()
在你的主题的任何其他模板文件(如 index.php
, single.php
, page.php
等)中,使用 get_header()
和 get_footer()
函数来包含页眉和页脚模板。
<?php get_header(); ?>
<!-- 这里是页面的主要内容 -->
<?php get_footer(); ?>
通过这种方式,你可以确保页眉和页脚在所有页面中保持一致,同时简化了模板代码的维护工作。当你需要更新页眉或页脚时,只需修改 header.php
或 footer.php
文件,所有的页面都会自动更新。