0

我的 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,我该如何解决这个问题?我正在使用的插件已经以这种方式工作,只有这部分会出现问题

4

1 回答 1

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>';
        }
    }
    

现在我可以看到分页器了

于 2013-03-04T06:00:09.127 回答