3

我在创建一个 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(); ?>

非常感谢您的帮助!

4

1 回答 1

2

只需将表头从循环中取出,只在循环内包含表行;

    <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>   
于 2013-04-22T07:34:42.573 回答