简体   繁体   English

如何将wordpress网站的页眉和页脚包含到自定义php文件中?

[英]How to include the header and the footer of wordpress website to a custom php file?

I have a custom php file that I want to include the header.php and the footer.php files to it , The file is located inside the theme files next to header.php and footer.php , but when I try 我有一个自定义php文件,我想将header.php和footer.php文件包括到其中,该文件位于header.php和footer.php旁边的主题文件中,但是当我尝试时

<?php get_header(); ?>

I get an error , Undefined function get_header. 我收到一个错误,未定义函数get_header。

Is there is a way to include it or I would have to use include/require to include them manually? 有没有一种方法可以包含它,或者我必须使用include / require手动包含它们?

Did you create those files in your project? 您是否在项目中创建了这些文件? You should create a footer.php and header.php on your project. 您应该在项目上创建一个footer.phpheader.php

Your page should look something like so: 您的页面应如下所示:

<!-- The header of the page -->
<?php get_header(); ?>
<!-- The main wrapper of the page -->
<main class="main-container container_960">
    <!-- Main content Section -->
    <section class="main-content">
        <!-- Printing the content -->
        <?php if (have_posts()) : while (have_posts()) :the_post() ?>

            <?php
                    the_title("<h1>","</h1>");

                    the_content();
            ?>

        <?php endwhile; else : ?>
            <!-- Showing that there is no content -->
            <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>

    </section>
    <!-- Pinting the Sidebar -->
    <?php get_sidebar(); ?>

</main>
<!-- Printing the footer -->
<?php get_footer() ?>

Create a file for the header, call it header.php : 为标题创建一个文件,将其称为header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php  the_title();?></title>
    <!-- This is for adding the meta information, where styles registered in functions php --> 
    <?php wp_head(); ?>
</head>
<body>

<header class="the_header">
<!-- You can create your content here
</header>
...

And you should create another file for the footer,again footer.php : 同样,您还应该为footer创建另一个文件footer.php

<footer class="main-footer">
        The footer
    </footer>

    <!-- We call here the scripts enqeued in functions.php-->
    <?php wp_footer();?>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM