2017-06-07 93 views
0

我在我的網站上使用WP初學者分頁(無插件),但我無法訪問第二頁和其他頁面。Wordpress分頁 - 無法訪問第二頁

<?php 

$category = get_category(get_query_var('cat')); 
$cat_id = $category->cat_ID; 

$custom_query_args = array(
     'post_type' => array('tutorials','post'), 
     'posts_per_page' => 2, 
     'cat' => $cat_id, 
); 

// Get current page and append to custom query parameters array 
$custom_query_args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1; 

// Instantiate custom query 
$custom_query = new WP_Query($custom_query_args); 

// Pagination fix 
$temp_query = $wp_query; 
$wp_query = NULL; 
$wp_query = $custom_query; 

// Output custom query loop 
if ($custom_query->have_posts()) : 
     while ($custom_query->have_posts()) : 
         $custom_query->the_post(); 

      echo '<article class="other-post col-xs-12 col-sm-12 col-md-3 col-lg-3">'; 
      echo '<div class="back-color">'; 
      echo '<h3>'; 
      echo the_post_thumbnail('post-thumbnail', array('class' => 'post-image')); 
      echo '<a href="'. get_permalink() .'" title="'. get_the_title() .'">'; 
      echo '<span><b>'. get_the_title() .'</b></span>'; 
      echo '</a>'; 
      echo '</h3>'; 
      echo '</div>'; 
      echo '</article>'; 

     endwhile; 
endif; 

// Reset postdata 
wp_reset_postdata(); 

// Custom query loop pagination 
wpbeginner_numeric_posts_nav(); 


// Reset main query object 
$wp_query = NULL; 
$wp_query = $temp_query; 
?> 

這裏是function.php的代碼,但我認爲這不是問題。 http://virtual-wizard.eu/function.txt

+0

我看到你正在使用wpbeginner的代碼..我測試了自定義主題上的代碼。我已經制作了一個教程。 (已測試) https://prabinparajuli.com.np/add-custom-pagination-in-wordpress/ –

+0

您的解決方案也出現同樣的錯誤。 – CRooY3

+0

您是否沖洗了您的永久鏈接?我認爲404錯誤是因爲你的永久鏈接沒有被刷新。 前往永久鏈接,只需保存。你會看到它的工作:) –

回答

1

您錯過了查詢參數中的paged參數。

我還要更換頁面的查詢,所以不是這樣的:

// Get current page and append to custom query parameters array 
$custom_query_args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1; 

你有這樣的:

// Get current page and append to custom query parameters array 
if (get_query_var('paged')) { 
    $paged = get_query_var('paged'); 
} elseif (get_query_var('page')) { 
    $paged = get_query_var('page'); 
} else { 
    $paged = 1; 
} 

,然後更改您的查詢到這一點:

$custom_query_args = array(
    'post_type' => array('tutorials','post'), 
    'posts_per_page' => 2, 
    'cat' => $cat_id, 
    'paged' => $paged, 
); 

我希望有所幫助。

+0

不,還是有同樣的問題。當我點擊頁面2時,404頁面沒有找到。 – CRooY3

+0

好吧,沒有能夠潛入您的代碼,我建議您查看您的固定鏈接的結構。如果您在分頁中獲得404,那往往會成爲一個問題。你是否熟悉WordPress的永久鏈接?如果不是,這裏是一些[來自Codex的有用信息](https://codex.wordpress.org/Using_Permalinks)。 –