在WordPress中,get_header()
和 get_footer()
是两个非常常用的模板函数,它们分别用于包含页眉(header)和页脚(footer)模板。以下是如何在WordPress主题中使用这两个函数的步骤:
-
创建页眉模板:
在你的WordPress主题文件夹中,创建一个名为header.php
的文件。这个文件将包含你的网站的页眉部分,通常包括网站的标题、导航菜单、徽标等。<!-- header.php --> <!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo( 'charset' ); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="profile" href="http://gmpg.org/xfn/11"> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <header> <h1><?php bloginfo( 'name' ); ?></h1> <nav> <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?> </nav> </header>
-
创建页脚模板:
同样,在你的WordPress主题文件夹中,创建一个名为footer.php
的文件。这个文件将包含你的网站的页脚部分,通常包括版权信息、链接到隐私政策页面、社交媒体图标等。<!-- footer.php --> <footer> <p>© <?php echo date('Y'); ?> <?php bloginfo( 'name' ); ?></p> <?php wp_footer(); ?> </footer> </body> </html>
-
在主模板文件中使用
get_header()
和get_footer()
:
在你的主题的主模板文件(如index.php
、page.php
、single.php
等)中,使用get_header()
和get_footer()
函数来包含页眉和页脚模板。<!-- index.php --> <?php get_header(); ?> <div id="content"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <article> <h2><?php the_title(); ?></h2> <?php the_content(); ?> </article> <?php endwhile; endif; ?> </div> <?php get_footer(); ?>
通过这种方式,你的页眉和页脚内容将在所有使用 get_header()
和 get_footer()
函数的模板页中保持一致,这有助于维护和更新你的WordPress主题。当你更新 header.php
或 footer.php
文件时,所有使用这些函数的页面都会自动反映出这些更改。