wordpress 页面共用 footer header

文章目录

    如何实现类似 Laravel Blade 模板系统中的 layout 功能呢?

    <?php get_header(); ?>
    <?php get_footer(); ?>
    

    这两行代码会分别获取主题目录下 header.php, footer.php 两个文件的输出。

    sidebar

    <?php get_sidebar(); ?>
    

    会获取主题目录下 sidebar.php 的输出。

    footer.php 的代码结构

    以 wordpress 4.7.3 的内置主题 twentyseventeen 为例,其 footer.php 内容如下

    <?php
    /**
     * The template for displaying the footer
     */
    
    ?>
    
    		</div><!-- #content -->
    
    		<footer id="colophon" class="site-footer" role="contentinfo">
    			<div class="wrap">
    				<?php
    				get_template_part( 'template-parts/footer/footer', 'widgets' );
    				get_template_part( 'template-parts/footer/site', 'info' );
    				?>
    			</div><!-- .wrap -->
    		</footer><!-- #colophon -->
    	</div><!-- .site-content-contain -->
    </div><!-- #page -->
    <?php wp_footer(); ?>
    
    </body>
    </html>
    

    header.php 的代码结构

    依旧以 twentyseventeen 为例, header.php 内容如下

    <?php
    /**
     * The header for our theme
     */
    
    ?><!DOCTYPE html>
    <html <?php language_attributes(); ?> class="no-js no-svg">
    <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(); ?>>
    <div id="page" class="site">
    	<a class="skip-link screen-reader-text" href="#content"><?php _e( 'Skip to content', 'twentyseventeen' ); ?></a>
    
    	<header id="masthead" class="site-header" role="banner">
    
    		<?php get_template_part( 'template-parts/header/header', 'image' ); ?>
    
    		<?php if ( has_nav_menu( 'top' ) ) : ?>
    			<div class="navigation-top">
    				<div class="wrap">
    					<?php get_template_part( 'template-parts/navigation/navigation', 'top' ); ?>
    				</div><!-- .wrap -->
    			</div><!-- .navigation-top -->
    		<?php endif; ?>
    
    	</header><!-- #masthead -->
    
    	<?php
    	// If a regular post or page, and not the front page, show the featured image.
    	if ( has_post_thumbnail() && ( is_single() || ( is_page() && ! twentyseventeen_is_frontpage() ) ) ) :
    		echo '<div class="single-featured-image-header">';
    		the_post_thumbnail( 'twentyseventeen-featured-image' );
    		echo '</div><!-- .single-featured-image-header -->';
    	endif;
    	?>
    
    	<div class="site-content-contain">
    		<div id="content" class="site-content">
    

    参考

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式