我正在尝试将一个小的导航模板文件包含到wordpress的页面中,所以我不需要在一堆页面中更改它。
下面是我的代码:<div id="content_top" class="whiteBG">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php get_template_part( 'subnav' ); ?>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium itaque dolores labore, illo nam. Aut voluptatum dicta sed sint rerum dignissimos vitae. Repellendus, harum tempora rem fugit.
</div>
</div>
</div>
</div>
我试图包含的代码是这个
<ul>
<li><a href="">button</a></li>
<li><a href="">button</a></li>
<li><a href="">button</a></li>
<li><a href="">button</a></li>
</ul>
get_template_part()函数扫描您的主题和子主题目录中的文件。
你用对了:
<?php get_template_part( 'subnav' ); ?>
将在你的themes目录中查找subnav.php。
既然你正确地使用了subnav.php,那么也许subnav.php在主题根目录中不存在,或者其他事情正在发生。
主函数就是您使用的get_template_part()。你也可以包括正常的东西,这取决于它在哪里被使用,使用require_once()或file_get_contents()。
<?php require_once('path/to/file.php'); ?>
<?php echo file_get_contents('path/to/file.html'); ?>
Require允许你传递php变量。如果使用文件获取内容,它将获取内容或代码。