Stefan
Stefan

Reputation: 11

How can I edit a WP custom template file with a custom table?

Creating a custom template file in WP which does the same as the standard template file is rather simple, but when I try to add an html table inside the customer template, then the table doesn't align with the rest of the theme (I use theme twentytwentyone as a test) and the table goes completely on the left side of the screen.

The code I started from, is the standard template for theme twentytwentyone:

<?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();

Maybe I should start from another template but i don't know which one. I cannot use the block editor in WP or a plugin to build up my table, because in the table there will be data from a custom mySQL table.

Or if you have better ideas on how to implement the above, I am happy to listen to what you have in mind. Thanks a lot everyone

Upvotes: 0

Views: 47

Answers (1)

Snuffy
Snuffy

Reputation: 2096

Each solution depends on what you need.

Solution 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();

Solution 2

Open file content-page.php located in yourtheme/template-parts/content and add your table there

<?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();

Solution 3

Create new template in yourtheme/template-parts/content for example content-table.php and load it

<?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();

Upvotes: 1

Related Questions