4

我尝试了多种解决方案,但它不起作用。

当我转到 /page/2 时,它不起作用。

我正在我的主题的 index.php 中执行自定义查询。

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$args = array(
    'post_type' => array('post', 'music', 'videos'),
    'post_status' => 'publish',
    //'meta_key' => 'featured',
    //'meta_value' => '1',
    'posts_per_page' => 10,
    'orderby'=>'date',
    'order'=>'DESC',
    'paged' => $paged
);

query_posts($args);

这是我网站的链接:我的网站主页

此页面不工作(抛出 404) -页面不工作(格式 - mywebsite/page/2/)

刚刚意识到此页面 2 有效 -正在工作的页面(格式为 - mywebsite.com/?page=2)

4

4 回答 4

16

I was having the same problem and this fixed everything for me. This allows me to paginate on index.php as well as my page.php with pretty permalinks.

HTML/PHP:

<?php
    //Fix homepage pagination
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

    $temp = $wp_query;  // re-sets query
    $wp_query = null;   // re-sets query
    $args = array( 'post_type' => array('post', 'music', 'videos'), 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 10, 'paged' => $paged);
    $wp_query = new WP_Query();
    $wp_query->query( $args );
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

<!--your loop stuff here -->

<?php endwhile; ?>
<nav>
   <?php paginate(); ?>
   $wp_query = null;
   $wp_query = $temp; // Reset
</nav>

This allows several things. One it checks if your on home, page, or single and tells the $paged variable how to react in turn. It also allows you to query your pagination with custom post types. Also by not using query_post you get to avoid some really funky stuff that you sometimes get when using it. The paginate(); is a custom function which we bring in now:

Inside your functions.php

function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'list',
    'next_text' => '&raquo;',
    'prev_text' => '&laquo;'
    );

if( $wp_rewrite->using_permalinks() )
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
}

This originally came from http://bavotasan.com/2011/simple-pagination-for-wordpress/ with me slightly modding it to get the pagination to work on the homepage.

This, again, does several things. It paginates your page with each page getting it's own link (which I find nice) and it also re-writes the URL to allow for pretty permalinks. If you check the link, the variable 's' was being used in place of 'paged' in part of this. I replaced the 's' with 'paged' and everything worked perfectly on my end.

Optional Styling of Pagination

ul.page-numbers {
    margin: 20px 0 10px;
    width: 100%;
    padding: 0;
    font-size: 12px;
    line-height: normal;
    clear: both;
    float: left;
}

ul.page-numbers li {
       float: left;
    }

ul.page-numbers a,
ul.page-numbers span {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    background: -webkit-gradient(linear, left top, left bottom, from(#E4E3E3), to(#FFFFFF));
    background: -moz-linear-gradient(top,  #E4E3E3,  #FFFFFF);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#E4E3E3', endColorstr='#FFFFFF');
    padding: 3px 4px 2px 4px; 
    margin: 2px;
    text-decoration: none;
    border: 1px solid #ccc;
    color: #666;
}

ul.page-numbers a:hover,
ul.page-numbers span.current {  
    border: 1px solid #666;
    color: #444;
}

Edit I realized afterwards that home page pagination would break after clicking one of the page tabs. I've fixed this by replacing the conditional statement and putting this in its place. I've updated my code above as well.

$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );
于 2012-12-13T09:05:24.233 回答
3

我有时也无法让分页正常工作。尝试使用下面的查询,看看是否有帮助。我刚刚添加了基本上一行代码并将您的查询更改为不使用该$args变量。

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts( array(
    'post_type' => array(
        'post',
        'music',
        'videos',
    ),
    'post_status' => 'publish',,
    'posts_per_page' => 10,
    'orderby'=>'date',
    'order'=>'DESC',
    'paged' => $paged )
);

if (have_posts()) : while (have_posts()) : the_post();

?> 

    // Display your content here    

    <?php the_title ?>

    <?php the_content(); ?>

<?php endwhile; endif; ?>
于 2012-12-07T19:17:15.250 回答
1

这里有几件事,永远不要在您的主页或任何存档页面上更改自定义查询的主查询。你总是会遇到分页问题。

此外,切勿用于query_posts创建自定义查询。我的重点,就像从不一样。

注意:此功能不适用于插件或主题。正如稍后解释的,有更好、性能更高的选项来更改主查询。query_posts() 是通过用新的查询实例替换页面的主查询来修改页面的主查询的过于简单和有问题的方法。它效率低下(重新运行 SQL 查询)并且在某些情况下会彻底失败(尤其是在处理帖子分页时)。

如果您需要更改主页或任何存档页面的行为,请使用pre_get_posts`在执行之前更改/修改主查询。这是更改主查询的正确方法。通过在执行之前更改主查询,您不会遇到分页问题。

pre_get_posts可以与条件标签结合使用以定位特定页面。查看为示例提供的链接。

于 2014-08-15T13:05:29.453 回答
1

我已经为这个问题苦苦挣扎了一段时间,结果我们的 WordPress 没有为我设置一个必填字段,max_num_pages.

所以我在循环中设置了这个变量。这是我的示例代码。

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('post_type' => 'cpt', 'paged'=>$paged, 'posts_per_page' => 3);
$loop = new WP_Query($args);
$GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages; // This is the line that did the magic for me.

经过进一步阅读,我发现了这一点;

  1. 在 single.php、singular.php 和其他单视图模板上:(int) $page是帖子的页面,由查询 var page: 指定get_query_var( 'page' )
  2. 在各种 archive.php 和类似的存档/帖子类型列表视图模板(int) $paged上: 全局变量是否包含帖子列表的页码(如存档中)。

这意味着如果你在一个页面模板(这是一个单一的视图模板)上并且正在使用一个循环,你将需要设置这个变量。

于 2014-08-15T11:32:41.687 回答