WP_Query分页不起作用

时间:2013-03-04 05:47:33

标签: wordpress pagination

我有wordpress的问题,我有这篇文章

$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');

$contacts = new \WP_Query('post_type=contact&showposts=10&paged='.$paged);

    if($contacts->have_posts()) {
        echo '<table><thead><tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Birthday</th></tr><thead><tbody>';
        while($contacts->have_posts()) {
            $contacts->the_post();
            echo '<tr>';
            echo '<td>'.get_post_meta($contacts->post->ID , 'contact_first_name' , true).'</td>';
            echo '<td>'.get_post_meta($contacts->post->ID , 'contact_last_name' , true).'</td>'; 
            echo '<td>'.get_post_meta($contacts->post->ID , 'contact_email' , true).'</td>';
            echo '<td>'.get_post_meta($contacts->post->ID , 'contact_birthday' , true).'</td>';
            echo '</tr>';
        }
        echo '</tbody></table>';

        if ( get_next_posts_link() || get_previous_posts_link() ) {
            echo '<div class="wp-navigation clearfix">
            <div class="alignleft">'.next_posts_link('&laquo; Older Entries').'</div>
            <div class="alignright">'.previous_posts_link('Newer Entries &raquo;').'</div>
            </div>';
        }

问题是分页不起作用,我尝试了一些解决方案,例如将$ contacts的名称更改为$ wp_query但是没有用,我也将showposts更改为posts_per_page但仍然无法正常工作,我认为问题是这里:\ WP_Query,我该如何解决这个问题?我正在使用的插件已经在以这种方式工作,只有这部分给出了问题

1 个答案:

答案 0 :(得分:4)

实际上我用这种方式解决了这个问题:

  1. 添加了全局$ wp_query
  2. 将$ contacts var重命名为$ wp_query

    global $wp_query;
    
    $paged = 1;
    if ( get_query_var('paged') ) $paged = get_query_var('paged');
    if ( get_query_var('page') ) $paged = get_query_var('page');
    
    $wp_query = new \WP_Query('post_type=contact&showposts=10&paged='.$paged);
    
    if($wp_query->have_posts()) {
        echo '<table><thead><tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Birthday</th></tr><thead><tbody>';
        while($wp_query->have_posts()) {
            $wp_query->the_post();
            echo '<tr>';
            echo '<td>'.get_post_meta($wp_query->post->ID , 'contact_first_name' , true).'</td>';
            echo '<td>'.get_post_meta($wp_query->post->ID , 'contact_last_name' , true).'</td>'; 
            echo '<td>'.get_post_meta($wp_query->post->ID , 'contact_email' , true).'</td>';
            echo '<td>'.get_post_meta($wp_query->post->ID , 'contact_birthday' , true).'</td>';
            echo '</tr>';
        }
        echo '</tbody></table>';
    
        if ( get_next_posts_link() || get_previous_posts_link() ) {
            echo '<div class="wp-navigation clearfix">
            <div class="alignleft">'.next_posts_link('&laquo; Older Entries').'</div>
            <div class="alignright">'.previous_posts_link('Newer Entries &raquo;').'</div>
            </div>';
        }
    }
    
  3. 现在我可以看到分页器