2013-03-04 79 views
0

我有WordPress的問題,我有這一塊WP_Query分頁不工作

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

的問題是,分頁不工作,我已經試過像改變的$聯繫人名稱$ wp_query一些解決方案但沒有工作,我也改變了showposts posts_per_page,仍然無法正常工作,我認爲問題在這裏:\ WP_Query,我該如何解決這個問題?我工作的插件以這種方式已經在工作,只有這一部分是給問題

回答

3

其實我用這種方式解決了這個:

  1. 加入全球$ wp_query
  2. 更名爲$接觸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>'; 
        } 
    } 
    

我可以看到分頁程序

+1

謝謝。它工作;) – itskawsar 2013-05-30 22:18:09

+0

我有同樣的問題,並使用您的解決方案,它的工作。這是奇怪的壽,如果您的查詢是在主頁上'頁面'查詢變量應該拿起頁碼會觸發分頁,但它不:\感謝反正:) – 2015-08-01 17:05:57