如何编辑带有自定义表的WP自定义模板文件?



在WP中创建一个与标准模板文件相同的自定义模板文件相当简单,但是当我尝试在客户模板中添加html表格时,表格不与主题的其余部分对齐(我使用theme twentytwentyone作为测试),表格完全在屏幕的左侧。

我开始的代码,是主题twenty - twenty - one的标准模板:

<?php
//  the_post();
get_template_part( 'template-parts/content/content-page' );
// my added table
?>
<table><th></th><th></th></table>
<?php
// end added table
// If comments are open or there is at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; // End of the loop.
get_footer();

也许我应该从另一个模板开始,但我不知道是哪个。我不能使用块编辑器在WP或插件来建立我的表,因为在表中会有来自自定义mySQL表的数据。

或者如果你有更好的想法如何实现以上,我很乐意听听你的想法。谢谢大家

每个解决方案都取决于您需要什么。

解决方案1

<?php
//  the_post();
get_template_part( 'template-parts/content/content-page' );
// my added table
//This is hard to manage if we work with alot of data or loops etc.
echo '<table><th></th><th></th></table>';
// end added table
// If comments are open or there is at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; // End of the loop.
get_footer();

解决方案2

打开位于yourtheme/template-parts/content目录下的content-page.php文件,并在其中添加表格

<?php
//  the_post();
get_template_part( 'template-parts/content/content-page' );
// end added table
// If comments are open or there is at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; // End of the loop.
get_footer();

解决方案3

在yourtheme/template-parts/content中创建新模板,例如content-table.php,并加载它

<?php
//  the_post();
get_template_part( 'template-parts/content/content-page' );
get_template_part( 'template-parts/content/content-table' );
// end added table
// If comments are open or there is at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; // End of the loop.
get_footer();