通过wordpress限制表格中显示的行数(php(

时间:2014-10-17 13:47:14

标签: php wordpress

我在wordpress中有一张表,它会越来越长。

我想设置限制,因此只显示最后250个结果或仅显示最后30天。无论更简单。

此代码来自Wordpress Affiliate Manager插件

    <p><?php printf(__( 'Displaying %1$d of %2$d impressions', 'wpam' ), count($this->viewData['impressions']), $this->viewData['impressionCount']); ?></p>

    <table class="widefat">
        <thead>
        <tr>
             <th width="25"><?php _e( 'ID', 'wpam' ) ?></th>
             <th width="200"><?php _e( 'Date Occurred', 'wpam' ) ?></th>
             <th width="100"><?php _e( 'Creative', 'wpam' ) ?></th>
             <th><?php _e( 'Referrer', 'wpam' ) ?></th>
        </tr>
        </thead>
        <tbody>
        <?php
        $creativeNames = $this->viewData['creativeNames'];

        foreach ( $this->viewData['impressions'] as $impression ) {
        ?>
        <tr class="impression">
            <td><?php echo $impression->impressionId?></td>
            <td><?php echo date("m/d/Y H:i:s", $impression->dateCreated)?></td>
            <td><?php echo $creativeNames[$impression->sourceCreativeId]?></td>
            <td><?php echo $impression->referer?></td>
        </tr>
        <?php } ?>

        </tbody>
    </table>
    <?php
     if ( ! count( $this->viewData['impressions'] ) ):
    ?>
         <div class="daterange-form"><p><?php _e( 'No records found for the date range selected.', 'wpam' ) ?></p></div>
    <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

您应该检查插件上是否有用于限制展示次数的选项。如果没有,您可以通过使用array_slice对数组进行切片来限制它,如下所示:

<?php
    $creativeNames = $this->viewData['creativeNames'];
    $impressions = array_slice($this->viewData['impressions'], 0, 250);

    foreach ( $impressions as $impression ) {
?>
<tr class="impression">
    <!-- your code -->
</tr>
<?php } ?>

如果您只需要最后250个,则可以将其与array_reverse

结合使用
$impressions = array_slice(array_reverse($this->viewData['impressions']), 0, 250);