drdyszuy488152 2015-11-17 15:10
浏览 36
已采纳

如何计算仍然使用PHP?

I'm using WordPress to display some posts in a table and I have a pagination along with this. The table contains a column named "Number" and a column for post title. My problem is that I don't know how to number every post when I'm on the next page because the number of post starts all over instead of the count where remained. How would I resolve this?

The count it doesn't start from where it should when I go to the next page. My code:

<?php 
require('wp-load.php');

if ( is_user_logged_in() ) {
    global $wp_query;
    $paged = (isset($_GET['pagina'])) ? $_GET['pagina'] : 1;
    $args = array(
        'posts_per_page' => 2,
        'paged' => $paged,
        'post_type' => 'post'
    );
    
    $the_query = new WP_Query( $args );
   
    echo '<table>';
    $count = $paged - 1;
    echo '<tr>
                <td width="100">Number</td>
                <td width="200">Title</td>
          <tr>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            $count++;   
            //var_dump($count); 
        echo '<tr>';
            echo '<td>'.$count .'</td>';
            echo '<td>'.the_title().'</td>';  
        echo '</tr>';
        }
    echo '</table>';
    
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
                        'format'       => '?pagina=%#%',
                        'current' => max( 1, $paged ),
                        'total' => $the_query->max_num_pages,
                        'prev_next'    => True,
                        'prev_text'    => __('Previous'),
                        'next_text'    => __('Next'),
                    ) ); 
}
?>

</div>
  • 写回答

1条回答 默认 最新

  • duanmangxie7131 2015-11-17 15:16
    关注
    $count = (($paged -1) * 2) + 1;
    

    where "2" is the posts_per_page val.

    EDIT: here is the whole code:

    $count = (($paged -1) * 2) + 1;
    echo '<tr>
                <td width="100">Number</td>
                <td width="200">Title</td>
          <tr>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<tr>';
        echo '<td>'.$count .'</td>';
        echo '<td>'.the_title().'</td>';  
        echo '</tr>';
        $count++;   
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错