0
votes

i have a problem with wordpress, i have this piece

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

the problem is that the pagination is not working, i have tried some solutions like changing the name of $contacts to $wp_query but didn't work, also I changed showposts to posts_per_page and still not working, i think the problem is here: \WP_Query, how can i fix this? the plugin i'm working is on is already working in this way and only this part is giving problems

1

1 Answers

4
votes

actually i resolved this in this way:

  1. added global $wp_query
  2. renamed $contacts var to $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>';
        }
    }
    

now i can see the paginator