我在创建一个wordpress页面模板时遇到了一些困难,该模板会输出一个包含我需要的数据的表格。
我目前所有信息都正确输出但是,每个循环都会重新显示表格和标题。我可以将所有数据循环到一个表中吗?
我的页面模板是:
<?php
/**
* Template Name: GamesDBTable
*
* Selectable from a dropdown menu on the edit page screen.
*/
?>
<?php get_header(); ?>
<div id="container">
<div id="content">
<?php
$type = 'game';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'orderby'=> 'title',
'order' => 'ASC',
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
$game_identifier = get_game_identifier();
$developer = strip_tags( get_the_term_list( $wp_query->post->ID, 'developer', '', ', ', '' ) );
$genre = strip_tags( get_the_term_list( $wp_query->post->ID, 'genre', '', ', ', '' ) );
$payment = get_field('th_payment_model');
$arating = get_post_meta( $wp_query->post->ID, 'rating_average');
$rating = (($arating[0]) / 10);
;?>
<h4>Games Database</h2>
<table class="publisher">
<thead>
<tr>
<th>Game Name <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Genre <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Payment Model <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Developer <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Rating <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href='<?php the_permalink() ?>'
rel='bookmark' title='<?php the_title(); ?>'>
<?php the_title(); ?></a></td>
<td><?php echo $genre; ?></td>
<td><?php echo $payment; ?></td>
<td><?php echo $developer; ?></td>
<td><?php print_r($rating); ?>/10</td>
</tr>
</tbody>
</table>
<?php
endwhile; endif; ?>
<!-- PAGINATION --><hr class="light"/><br style="clear:both;"/><div class="aligncenter"><?php echo vpt_pagination(); ?></div>
</div><!-- #content -->
<?php get_sidebar(); ?>
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
非常感谢帮助!
答案 0 :(得分:1)
只需将表头从循环中取出,只在循环中包含表行;
<table class="publisher">
<thead>
<tr>
<th>Game Name <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Genre <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Payment Model <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Developer <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
<th>Rating <a href="#" class="asc">a</a> <a href="#" class="desc">d</a></th>
</tr>
</thead>
<tbody>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$game_identifier = get_game_identifier();
$developer = strip_tags( get_the_term_list( $wp_query->post->ID, 'developer', '', ', ', '' ) );
$genre = strip_tags( get_the_term_list( $wp_query->post->ID, 'genre', '', ', ', '' ) );
$payment = get_field('th_payment_model');
$arating = get_post_meta( $wp_query->post->ID, 'rating_average');
$rating = (($arating[0]) / 10);
?>
<tr>
<td><a href='<?php the_permalink() ?>'rel='bookmark' title='<?php the_title(); ?>'><?php the_title(); ?></a></td>
<td><?php echo $genre; ?></td>
<td><?php echo $payment; ?></td>
<td><?php echo $developer; ?></td>
<td><?php print_r($rating); ?>/10</td>
</tr>
<?php endwhile; endif; ?>
</tbody>
</table>