自定义循环中的分页不起作用 [英] Pagination in custom loop not working

查看:31
本文介绍了自定义循环中的分页不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了几个关于这个问题的主题和问题,但我没有找到任何适合我的代码的答案.

显示我的自定义帖子的分页,但是当我点击 Show next posts >> 时,会显示上一页的相同帖子,即使 URL 显示 ?paged=2.

我的代码如下:

<?php$args = array( 'post_type' => 'strn5_podcasts','posts_per_page' =>3、'分页' =>(get_query_var('paged') ? get_query_var('paged') : 1 );$temp = $wp_query;$wp_query = null;$wp_query = new WP_Query( $args);如果( $wp_query->have_posts() ):而( $wp_query->have_posts() ):$wp_query->the_post();?><div <?php post_class();?>><h4 class="podcast-title"><?php the_title();?></h4><div class="podcast-content"><?php the_content();?>

<h6><?php the_time('F j, Y');?></h6><div class="post-separator col-lg-12"></div>

<?php endwhile;万一;?><div class="navigation-links"><div class="next-post"><?php next_posts_link( '>> Siguiente Entrada');?>

<div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>');?>

<?php $wp_query = null;$wp_query = $temp;wp_reset_query();?>

<!-- .podcast-entries -->

解决方案

通常,当某些东西不起作用时,我会做的事情是将它从所有东西中剥离出来,并开始使用基本的基础知识重新构建它,以查看可能是什么问题.

为什么不尝试更简单的循环形式,看看是否可以将其打印到 prin.t

 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page')));if (have_posts()) : while (have_posts()) : the_post();?><p><?php the_title();?></p><?php endwhile;结束;?><div class="navigation-links"><div class="next-post"><?php next_posts_link('>> Siguiente Entrada');?></div><div class="previous-post"><?php previous_posts_link('Anterior Entrada >>');?></div>

这几乎与您正在做的事情(几乎)相同,但没有任何变量或任何其他可能"出错的事情.

编辑

最后证明 paged 参数可以与 pagedpage 一起使用,在这种情况下它适合你用 page 而不是 paged

所以它最终看起来像这样:

'paged' =>get_query_var('page')

最后一点,如果您在已设置为主页的自定义页面模板中使用查询帖子,则需要使用 get_query_var('page').

I have already searched several topics and questions regarding this problem, but I haven´t found any answer that suits my code.

The pagination for my custom posts is displayed, but when I click in Show next posts >> the same posts from the previous page are shown, even though the URL shows ?paged=2.

My code is the following:

<div class="podcast-entries">
    <?php
    $args = array( 'post_type' => 'strn5_podcasts',
                   'posts_per_page' => 3,
                   'paged' => (get_query_var('paged') ? get_query_var('paged') : 1 );
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );

    if ( $wp_query->have_posts() ) : 
    while ( $wp_query->have_posts() ) :
    $wp_query->the_post();
    ?>

        <div <?php post_class(); ?> >

            <h4 class="podcast-title"><?php the_title(); ?></h4>

            <div class="podcast-content">
                <?php the_content(); ?>
            </div>

            <h6><?php the_time('F j, Y'); ?></h6>

            <div class="post-separator col-lg-12"></div>

        </div>

    <?php endwhile; endif; ?>

    <div class="navigation-links">

        <div class="next-post">
            <?php next_posts_link( '>> Siguiente Entrada' ); ?>
        </div>

        <div class="previous-post">
            <?php previous_posts_link( 'Anterior Entrada >>' ); ?>
        </div>

    </div>

    <?php $wp_query = null; 
    $wp_query = $temp;
    wp_reset_query(); ?>

</div> <!-- .podcast-entries -->

解决方案

Usually what I do when something doesn't work is that I strip it from everything and start rebuilding it with bare basics to see what the problem might be.

Why don't you try a simpler form of loop to see if you can get it to prin.t

<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
  <p><?php the_title(); ?></p>
<?php endwhile; endif;?>

<div class="navigation-links">
  <div class="next-post"><?php next_posts_link( '>> Siguiente Entrada' ); ?></div>
  <div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>' ); ?></div>
</div>

It's pretty much the same thing you are doing (almost) but without any variables or any other thing that "could" go wrong.

Edit

In the end it turned out that the paged parameter can be used with paged or page and in this case it worked out for you with page instead of paged

So it ended up looking like so:

'paged' => get_query_var('page')

On a final note, using get_query_var('page') is needed if you're using query posts in a custom page template that you've set as your homepage.

这篇关于自定义循环中的分页不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆