2017-06-07 94 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)。 –