Search code examples
wordpresshtml-tableposts

Return Posts as single table


query_posts( array(
'post_type' => 'ts_result',
'meta_query' => array(
    array(
       'key' => '_ts_student_class',
       'value' => $exam_reg,
       'compare' => '='
    ),
    array(
       'key' => '_ts_school_term',
       'value' => $exam_term,
    ),
    array(
       'key' => '_ts_school_year',
       'value' => $exam_year,
    )
),
'posts_per_page' => 100
   )
);
?>

<table style="border:1px solid black" cellpadding="1.5" cellspacing="5">
<style type="text/css" media="print">

table{
    border-collapse:collapse;
}
</style>
<tbody>

<tr>
    <th><strong>NAME\SUBJECT</strong></th>
    <th><strong>English Studies</strong></th>
    <th align='center'><strong>Mathematics</strong></th>
    <th align='center'><strong>CCA</strong></th>
    <th align='center'><strong>Yoruba Language</strong></th>
    <th align='center'><strong>French</strong></th>
    <th align='center'><strong>Business Education</strong></th>
    </tr>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <?php 
     $subject_english_studies_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_english_studies_exam_total',true );
       $subject_maths_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_maths_exam_total',true );
      $subject_creative_arts_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_creative_arts_exam_total',true );
     $subject_yoruba_lang_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_yoruba_lang_exam_total',true );
     $subject_french_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_french_exam_total',true );
       $subject_business_edu_exam_total= get_post_meta( 
     get_the_ID(),'_ts_subject_business_edu_exam_total',true );
?> 

<tr>
    <td><?php the_title(); ?></td>
    <td><?php echo $subject_english_studies_exam_total ;?></td>
    <td><?php echo $subject_maths_exam_total ;?></td>
    <td><?php echo $subject_creative_arts_exam_total ;?></td>
    <td><?php echo $subject_yoruba_lang_exam_total ;?></td>
    <td><?php echo $subject_french_exam_total ;?></td>
    <td><?php echo $subject_business_edu_exam_total ;?></td>
   </tr>
</tbody>
</table>

the above query in Wordpress returns the following:

Screenshot of output

What I seek to achieve is a table with one header and the other post content as rows. presently only the first post values is being formatted properly as table rows.


Solution

  • You have done small mistake by not closing the while loop before the </tbody>. Code will be like the following :

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <tr>
       <td><?php the_title(); ?></td>
       <td><?php echo $subject_english_studies_exam_total ;?></td>
       <td><?php echo $subject_maths_exam_total ;?></td>
       <td><?php echo $subject_creative_arts_exam_total ;?></td>
       <td><?php echo $subject_yoruba_lang_exam_total ;?></td>
       <td><?php echo $subject_french_exam_total ;?></td>
       <td><?php echo $subject_business_edu_exam_total ;?></td>
     </tr>
    <?php endwhile;endif; ?>
    

    Hope it helps you.