• Resolved usmile0926

    (@usmile0926)


    Hi,
    How can I output posts in a table on a particular page? Right now, I have my posts displayed on a page called ‘News.’ But I want my posts listed in rows of a table, with 4 columns: title, date created, author, and hits. I wrote a php template with a while loop that would display the_title(), the_time(), the_author_posts_link(), and a short code from a hits counter plugin in a table, but I can’t connect this template to the posts. I did choose this template that I wrote in the ‘News’ page. But that didn’t do anything.

    Can anyone help me?

    Thanks,
    usmile0926

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey there,

    To put WordPress posts into an html table, in the file where you are editing code, start the HTML table’s opening tag:

    <table>

    Then, start running a custom query using this (http://codex.wordpress.org/Class_Reference/WP_Query)

    Make sure to include the columns and rows in the table for each loop iteration. For example, inside the loop for each one, you might do something like this:

    <tr>
        <td><?php the_title(); ?></td>
        <td><?php the_content(); ?></td>
        <td><?php the_date('Y-m-d'); ?></td>
     </tr>

    And then after the loop is done, close the html table again

    </table>

    Thread Starter usmile0926

    (@usmile0926)

    Hello,
    Thanks for responding so quickly.
    I thought that’s what I did. Here is my code.

    <?php
    /*
    Template Name: PostTable Template
    */
    ?>

    <table>
    <tr>
    <th>Title</th>
    <th>Created Date</th>
    <th>Author</th>
    <th>Hits</th>
    </tr>
    <?php while ( have_posts() ) : the_post(); ?>
    <tr>
    <td>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></td>
    <td><?php the_time(‘F jS, Y’); ?></td>
    <td><?php the_author_posts_link(); ?></td>
    <td><?php get_HitsMechanic();?></td>
    </tr>
    <?php endwhile; ?>
    </table>

    Isn’t this enough?

    You’re getting close – but you haven’t actually gotten the posts in order to loop through them. WordPress Page Templates don’t automatically have a “loop” built into them – like other standardized pages in WordPress Themes (like archive.php).

    So you need to do some extra setup in order to make the loop work.

    Check out that link I sent you because it has some great examples. Specifically, you need something like this in order to get the loops contents:

    //Set up the args we want to make this loop custom:
    $args = array(
    	'post_type' => 'post',
    );
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
    	echo '<ul>';
    	while ( $the_query->have_posts() ) {

    Your table stuff goes here and then we end the loop

    }
    }
    Thread Starter usmile0926

    (@usmile0926)

    Thanks a million!
    Now I got it working!
    Yay!
    You have a great day!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to display posts in a table?’ is closed to new replies.