简体   繁体   English

分页不适用于WP_query

[英]Pagination not working with WP_query

I'm trying to list the posts from the category 'alps'. 我正在尝试列出“阿尔卑斯”类别的帖子。 The problem is if I define 'posts_per_page'=> -1, that works fine and I get the complete list, but if I ask for a number of posts I just get the same posts repeating page after page. 问题是如果我定义'posts_per_page'=> -1,那很好,并且我得到了完整的列表,但是如果我要多个帖子,我只会得到相同的帖子,一页又一页地重复。 Here's my loop-alps.php file. 这是我的loop-alps.php文件。

<?php
$args = array(

'order' => 'asc',
'order_by' => 'title',
'posts_per_page'=> 5, 
'category_name'=> 'alps'
);

$wp_query = new WP_Query($args);

if($wp_query->have_posts()): while($wp_query->have_posts()): $wp_query->the_post();
echo '<h1>' .get_the_title() .'</h1>';
the_post_thumbnail();
endwhile;

endif;
?>



<div class="navigation">
<?php if(function_exists('tw_pagination')) tw_pagination($the_query); ?>
</div>

According to WP documentation (Codex) it seems that you should use the paged parameter in order to make it work correctly. 根据WP文档(Codex),似乎应该使用paged参数才能使其正常工作。

$args = array(

'order' => 'asc',
'order_by' => 'title',
'posts_per_page'=> 5, 
'category_name'=> 'alps',
'paged' => get_query_var( 'paged' )
);

get_query_var( 'paged' ) - this function basically look for a GET variable in the URL, '?paged=X' if i'm not mistake. get_query_var( 'paged' ) -此函数基本上在URL中查找GET变量,如果我没有get_query_var( 'paged' )查找'?paged = X'。 So make sure that by clicking on the pagination links you can see that this parameter is being added to the URL and is being changed accordingly. 因此,请确保通过单击分页链接,您可以看到此参数已添加到URL,并且已相应更改。

Source: https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters 资料来源: https : //codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

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

'order' => 'asc',
'order_by' => 'title',
'posts_per_page' => 12,
'category_name' => 'alps',
'paged' => $paged,
'offset'=> 1
);

Just add 'paged' => get_query_var( 'paged' ), like this, adjust it to your own 只需添加'paged' => get_query_var( 'paged' ),像这样,将其调整为您自己的即可

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$myposts = array(
    'showposts' => 6,
    'post_type' => 'your-post-type',
    'orderby' => 'date',
    'paged' => get_query_var( 'paged' ),
    'tax_query' => array(
        array(
        'taxonomy' => 'your-taxonomy',
        'field' => 'slug',
        'terms' => 'your-terms')
    )
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($myposts);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM